將基於ASP.NET SOAP的Web服務轉換爲基於JSON的響應的正確方法是什麼? ...然後從jQuery調用這些?ASP.NET - 使用jQuery的基於JSON的Web服務的正確方法是什麼?
當基於jQuery的AJAX和ASP.NET集成時,什麼是「最佳實踐」? 文章?圖書?
將基於ASP.NET SOAP的Web服務轉換爲基於JSON的響應的正確方法是什麼? ...然後從jQuery調用這些?ASP.NET - 使用jQuery的基於JSON的Web服務的正確方法是什麼?
當基於jQuery的AJAX和ASP.NET集成時,什麼是「最佳實踐」? 文章?圖書?
JSON轉換到.NET類可以用System.Runtime.Serialization和System.Runtime.Serialization.JSON來完成。我懷疑你對設置從客戶端到服務器的函數調用更感興趣。我認爲這值得嘗試this tutorial。
在本教程中,您需要添加一個web服務'.asmx'文件。在asmx文件中,您將能夠創建可從客戶端腳本調用的函數。您的ASP.NET頁面也可以引用生成的客戶端腳本來調用.asmx函數。
如果你真的想要做的JSON序列不過,你也可以使用以下命令:
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
public class JsonSerializer
{
// To make a type serializeable, mark it with DataContractAttribute
// To make a member of such types serializeable, mark them with DataMemberAttribute
// All types marked for serialization then need to be passed to JsonSerialize as
// parameter 'types'
static public string JsonSerialize(object objectToSerialize, params Type[] types)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(
types[0], types.Skip(1));
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, objectToSerialize);
ms.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
return sr.ReadToEnd();
}
}
以下關於使用自定義的方法codproject.com會談文章Extending an existing ASP.NET Web Service to support JSON由鮑比·蘇亞雷斯屬性來達到期望的結果。
我已經使用ASP.Net Ajax一段時間了,但是不用擔心JSON或XML通信。相反,我使用Web服務直接返回可以使用innerHTML設置的內容。
這很容易實現。只需創建一個Web服務(ASMX)文件,並將您的方法聲明爲WebMethods(設置WebMethod屬性)。
現在,您可以從Javascript代碼中調用Web服務,就像常規函數一樣。
函數的結果將返回給回調函數。這是結構
//Webmethod returns some HTML content
Myservice.DoSomething(myParam, callBackFunction);
//Content is set on the webpage
function callBackFunction(result){
document.getElementById('myElemID').innerHTML = result;
}
爲什麼不只是使用更新面板,如果你將有傳遞HTML的開銷,而不是輕量級的json對象? json的想法是保持k的電線最小,因此具有快速的帶外響應 – redsquare 2008-11-12 07:46:27
鮑比索雷斯的文章看起來很完美。我很想嘗試實現它,因爲它看起來會支持我的項目。 – htm11h 2012-10-03 14:04:50