调用带参数的webservice返回XML数据一个小细节

新生在二个不起眼的小站找到二个帖子,某一个人的叁个建议晋升了自家。
本身本来的代码是那样写的:
错误代码

jquery中通过ajax调用webservice传递数组参数的主题材料,jquerywebservice

如题.

要么一向用例子表达来的直接些.

调用带参数的webservice返回XML数据一个小细节。小编的项目中通过jquery.ajax调用webservice.

客户端代码如下: 图片 1

 1             $.ajax({
 2                 url: "test/xxx.asmx",
 3                 type: 'POST',
 4                 dataType: 'xml',
 5                 timeout: 1000,
 6                 data: { name: "zhangsan", tags: ["aa", "bb", "cc"] },
 7                 error: function(xml) {
 8                     alert(xml.responseText);
 9                 },
10                 success: function(xml) {
11                     alert("OK");
12                 }
13 
14             });

图片 2

服务端代码如下:

1   [WebMethod]
2     public XmlDocument xxx(string name, string [] tags )
3     { 
4          return sth;  
5     }

连天抛出格外.

好端端须求基本格式

1         [WebMethod]
2         public string SayHello(string name)
3         {
4             return "Hello " + name;
5         }

 1             $.ajax({
 2                 url: "/CommonService.asmx/SayHello",
 3                 type: "Post",
 4                 dataType: "json",
 5                 contentType: "application/json; charset=utf-8",
 6                 data: "{name:'Varchar32'}",
 7                 success: function (data) {
 8                     alert(data.d);
 9                 },
10                 error: function (data) {
11                     //200的响应也有可能被认定为error,responseText中没有Message部分
12                     alert($.parseJSON(data.responseText).Message);
13                 },
14                 complete: function (data) {
15                     ;//after success or error
16                 }
17             });

复制代码 代码如下:

标题出将来这里:

下面是HTTP数据:

图片 3

POST http://xxx.com/xxx.asmx/xxx HTTP/1.1
Host: center.cmis.htpc.com.cn
Connection: keep-alive
Content-Length: 55
Cache-Control: max-age=0
Origin: http://xxx.com
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: application/xml, text/xml, */*; q=0.01
Referer: http://xxx.com/xxx.aspx
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3

name=zhangsan&tags%5B%5D=aa&tags%5B%5D=bb&tags%5B%5D=cc

图片 4

而它仰望的格式是之类的:

POST /xxx.asmx/xxx HTTP/1.1
Host: xxx.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length

name=string&tags=string&tags=string

相比较方面粗体,post的数码除了难点. 精确的应该如下:

name=zhangsan&tags=aa&tags=bb&tags=cc

总的看难点出在jquery.ajax上边了.见代码(jquery.1.8.3.js)

图片 5

 1     function buildParams(prefix, obj, traditional, add) {
 2         var name;
 3 
 4         if (jQuery.isArray(obj)) { 
 5             // Serialize array item.
 6             jQuery.each(obj, function(i, v) {
 7                 if (traditional || rbracket.test(prefix)) {                    
 8                     // Treat each array item as a scalar.
 9                     add(prefix, v);
10 
11                 } else {
12                     // If array item is non-scalar (array or object), encode its
13                     // numeric index to resolve deserialization ambiguity issues.
14                     // Note that rack (as of 1.0.0) can't currently deserialize
15                     // nested arrays properly, and attempting to do so may cause
16                     // a server error. Possible fixes are to modify rack's
17                     // deserialization algorithm or to provide an option or flag
18                     // to force array serialization to be shallow.
19                     
20                     //ytx 20130411
21                     buildParams(prefix, v, traditional, add);
22                     //buildParams(prefix + "[" + (typeof v === "object" ? i : "") + "]", v, traditional, add);
23                 }
24             });
25 
26         } else if (!traditional && jQuery.type(obj) === "object") {
27             // Serialize object item.
28             for (name in obj) {
29                 buildParams(prefix + "[" + name + "]", obj[name], traditional, add);
30             }
31 
32         } else {
33             // Serialize scalar item.
34             add(prefix, obj);
35         }
36     }

图片 6

中间多少个参数的意思

  type:乞求格局,又称Method

  dataType:预期重返类型(The type of data that you’re expecting back
from the server)

  contentType:发送到服务器的数码的编码类型(When sending data to the
server, use this content type)

  data:发送到服务器的多寡

$.ajax({
type: “post”,
url: “_service.asmx/getDataFromATable”,
data:” { tablename: temp }”,
dataType: “XML”

结论:

出标题标代码在22行,我修改成21行那样就行了.

不过,作者对js/jquery都以夏虫语冰的,希望不要引起别的后遗症,呵呵.

 

 

 

 

如题. 依然直接用例子表达来的直白些.
本身的项目中通过jquery.ajax调用webse…

归来各种类型的多寡

WS是那样写的:
webservice

 1、返回string

1         [WebMethod]
2         public string HelloWorld()
3         {
4             return "Hello World";
5         }

1     //js的其他部分略
2     success: function (data) {
3         $(".ajaxresult div:eq(0)").html("返回数据的类型为:" + typeof(data.d));
4         $(".ajaxresult div:eq(1)").html("内容为:" + data.d);
5     }    

浏览器看到的结果

图片 7

另附上选取text格式调用的代码

 1             $.ajax({
 2                 url: "/CommonService.asmx/HelloWorld",
 3                 type: "Post",
 4                 dataType: "text",
 5                 data: {},
 6                 success: function (data) {
 7                     $(".ajaxresult div:eq(0)").html("返回数据的类型为:" + typeof (data));
 8                     $(".ajaxresult div:eq(1)").html("内容为:" + data);
 9                 }
10             });

复制代码 代码如下:

 2、返回int

1         [WebMethod]
2         public int HelloWorld()
3         {
4             return 1;
5         }

js部分略

浏览器看到的结果

图片 8

[WebMethod]
public DataSet getDataFromATable(string tablename)
{
DataSet ds = new DataSet();
using (SqlConnection con=new SqlConnection(connectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = string.Format(“select * from {0}”,tablename);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
return ds;
}
[code]
网络搜到的,都说纵然是无参数的WS,用地点的data:”{}”是尚未错的,但有参的那样传会出错。
实则很轻便,只要求做一些细小修改就可以了
精确代码
[code]
$.ajax({
type: “post”,
url: “_service.asmx/getDataFromATable”,
data: { tablename: temp },
dataType: “XML”,

3、再次来到数组(List或Array)

1         [WebMethod]
2         public List<string> HelloWorld()
3         {
4             return new List<string>() { "Frozen_Zhang","Varchar32"};
5         }

 1             //js的其他部分略
 2             success: function (data) {
 3                     $(".ajaxresult div:eq(0)").html("返回数据的类型为:" + typeof (data.d));
 4                     var str = "";
 5                     $.each(data.d, function (index, val) {
 6                         str += "第" + index + "项:" + val + " ;";
 7                     });
 8                     str = str.substring(0,str.length - 1);
 9                     $(".ajaxresult div:eq(1)").html("内容为:" + str);
10                 }

浏览器看到的结果

图片 9

相关文章