jQuery ajax Webサービス: 取得と投稿
1. GET メソッド
var data = { classCode: "0001"} // JOSN オブジェクトはここで直接使用されます
$.ajax({
type: "GET "、
contentType: "application/json; charset=utf-8"、
url: "/WebServices/ProductPropertyWebService.asmx/GetProductPropertyList"、
dataType: "json"、
anysc: false ,
data: data,
success: RenderProperties,
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown ':' textStatus) // エラー処理
}
} ; 以下のように:
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)] //UseHttpGet = true
public List
{ string classCode = HttpContext.Current.Request["classCode"]; // クエリ文字列内のパラメータ値を取得するためのメソッドを取得します
return PropertyManager.GetPropertySet(classCode, "zh-CN").DataList; }
コードをコピー
コードは次のとおりです:
var data = '{ classCode: "' classCode '", city: "GuangDong" }'; // 結合された JOSNここでは文字列を使用する必要があります
$.ajax({ type: "POST",
contentType: "application/json; charset=utf-8", url: "/WebServices/ProductPropertyWebService. asmx/GetProductPropertyList",
dataType: "json",
anysc: false,
サーバー側
コード
コードをコピー
コードは次のとおりです。
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)] // UseHttpGet = false
public List
GetProductPropertyList(string classCode, string city) // Post メソッド、パラメータJSON フィールドのプロパティに対応し、 {
return PropertyManager.GetPropertySet(classCode, "zh-CN").DataList;
を使用して値を直接割り当てます。注: GET メソッドと POST メソッドとは異なり、パラメーターがある場合、パラメーターの値が ASCII 文字 (中国語など) ではない場合、GET パラメーターは encodeURI エンコードされている必要があります。そうしないと、サーバーが受信するデータが文字化けします。
クライアント:
コード
コードをコピー
コードは次のとおりです:
var productPropertyTemplate = {"ProductId":10024, "PropertyList":[
{"PropertyId":18, "PropertyType":" text", "PropertyValue ":"数値は 100"},
{"PropertyId":19, "PropertyType":"checkbox", "PropertyValue":"57|28"}]}
$。 ajax({
type: "GET", contentType: "application/json; charset=utf-8", url: "/WebServices/ProductPropertyWebService.asmx/PostProductPropertyList",
anysc: false、
data : { propertyList: productPropertyTemplate }、dataType: "json"、
success: function (result) {alert(result.d) }、error: function (XMLHttpRequest, textStatus) , errorThrown) { alert(errorThrown ':' textStatus) } });
Server side:
1. To deserialize Json characters into .net objects, there are many open source libraries. I use the DataContractJsonSerializer that comes with .net version 3.5 or above. Write an auxiliary class:
Code
///
/// Helper methods for Json serialization and deserialization
///
public class JsonHelper
{
/// /// JSON serialization: serialize objects into Json format strings
///
public static string JsonSerializer(T t)
{
var ser = new DataContractJsonSerializer(typeof(T));
var ms = new MemoryStream();
ser.WriteObject(ms, t);
string jsonString = Encoding.UTF8.GetString(ms .ToArray());
ms.Close();
return jsonString;
}
///
/// JSON deserialization: according to Json format String, deserialized into object
///
public static T JsonDeserialize(string jsonString)
{
var ser = new DataContractJsonSerializer(typeof(T)) ;
var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
var obj = (T)ser.ReadObject(ms);
return obj;
}
}
2. Because it needs to be deserialized into corresponding objects, two object classes are constructed first. Pay attention to the characteristic modifiers in front of each class and the field of the class:
Code
[DataContract]
public class MProductProperty
{
[DataMember(Order = 0, IsRequired = true)]
public int ProductId { set; get; }
[DataMember(Order = 1, IsRequired = true)]
public List PropertyList { set ; get; }
}
public class MProperty
{
[DataMember(Order = 0, IsRequired = true)]
public int PropertyId { set; get; }
[DataMember (Order = 1, IsRequired = true)]
public string PropertyType { set; get; }
[DataMember(Order = 2, IsRequired = true)]
public string PropertyValue { set; get; }
}
3. Web method for receiving and processing Json data:
Code
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string PostProductPropertyList()
{
string jsonString = HttpContext.Current .Request["propertyList"];
var productProperty = JsonHelper.JsonDeserialize(jsonString); // productProperty is successfully deserialized into an MProductProperty object
//Return the reception success indicator
return "postsuccess" ;
}