您好,欢迎访问一九零五行业门户网

jQuery结合Json提交数据到Webservice,并接收从Webservice返回的Json数据_jquery

jquery ajax webservice:get 和 post
一、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 getproductpropertylist()
{
string classcode = httpcontext.current.request[classcode]; // get 方式,要在查询字符串里得到参数值
return propertymanager.getpropertyset(classcode, zh-cn).datalist;
}
二、post 方式
客户端
代码
复制代码 代码如下:
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,
data: data, // post 方式,data参数不能为空,如果不传参数,也要写成{},否则contenttype将不能附加在request headers中。
success: renderproperties,
error: function (xmlhttprequest, textstatus, errorthrown) {
alert(errorthrown + ':' + textstatus); // 错误处理
}
});
服务器端
代码
复制代码 代码如下:
[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编码,要不服务端接收到的数据为乱码。
复杂的json数据提交
简单的json 格式的数据如 { name:yangjun, age:27 }
复杂的json格式的数据,其实只是json嵌套,比如: {name:yangjun, age:27, child:[{name:yangke, age:1},{name:yangbin, age:2}]}
如果是这种复杂的json格式的数据要提交,并在webservices中获取,然后根据这个json格式的字符串,序列成.net对象,应该怎么做呢?
比如我要提交下面的数据:
客户端:
代码
复制代码 代码如下:
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);
}
});
服务器端:
1、要反序列化json字符为.net对象,有比较多的开源类库,我使用的是.net 3.5版本以上自带的datacontractjsonserializer,写一个辅助类:
代码
复制代码 代码如下:
///
/// json序列化和反序列化的帮助方法
///
public class jsonhelper
{
///
/// json序列化:把对象序列化成json格式的字符串
///
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反序列化:根据json格式的字符串,反序列化成对象
///
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、因为要反序列化成相应的对象,所以先构造两个对象类,注意每个类和类的字段前面的特性修改符:
代码
复制代码 代码如下:
[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、接收并处理json数据的web方法:
代码
复制代码 代码如下:
[webmethod]
[scriptmethod(usehttpget = true)]
public string postproductpropertylist()
{
string jsonstring = httpcontext.current.request[propertylist];
var productproperty = jsonhelper.jsondeserialize(jsonstring); // productproperty 成功反序列化成mproductproperty的对象
//返回接收成功标识
return postsuccess;
}
其它类似信息

推荐信息