在調(diào)試jQuery帶參數(shù)的ajax調(diào)用WebService的時(shí)候,被參數(shù)的問題搞了一下,問題是這樣的:
首先在服務(wù)器端(C#, .NET2.0)定義了一個(gè)WebService, 里面有兩個(gè)方法,其中一個(gè)返回:List<Person> GetPersonList(string input), 返回的是自定義的集合。
WebService代碼如下:
1: using System;
2: using System.Collections.Generic;
3: using System.Collections.ObjectModel;
4: using System.Web;
5: using System.Collections;
6: using System.Web.Services;
7: using System.Web.Services.Protocols;
8:
9: [WebService(Namespace = "http://semenoff.dk/")]
10: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
11: [System.Web.Script.Services.ScriptService()]
12: public class MySampleService : System.Web.Services.WebService
13: {
14: public MySampleService()
15: {
16: //Uncomment the following line if using designed components
17: //InitializeComponent();
18: }
19:
20: [WebMethod]
21: public string GetServerResponse(string callerName)
22: {
23: if(callerName== string.Empty)
24: throw new Exception("Web Service Exception: invalid argument");
25:
26: return string.Format("Service responded to {0} at {1}", callerName, DateTime.Now.ToString());
27: }
28:
29:
30: [WebMethod]
31: public List<Person> GetPersonList(string input)
32: {
33: List<Person> ret = new List<Person>();
34:
35: Person p = new Person();
36: p.ID = "001";
37: p.Value = "Jason Hu";
38:
39: ret.Add(p);
40:
41: p = new Person();
42: p.ID = "002";
43: p.Value = "Thomas Li";
44:
45: ret.Add(p);
46:
47: //System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
48: //return json.Serialize(ret);
49:
50: return ret;
51: }
52:
53: }
54:
55: public class Person
56: {
57: public string ID { get; set; }
58: public string Value { get; set; }
59: }
60:
客戶端jQuery用ajax調(diào)用這個(gè)WebService,其中jQuery的ajax的data參數(shù)是常量可以:
1: $.ajax({
2: type: "POST",
3: contentType: "application/json",
4: url: "./WebServices/MySampleService.asmx/GetServerResponse",
5: data:"{input:56}", // ************* 常量 ******************
6: dataType: 'json',
7: success: function(result) {
8: alert("jQuery callback: " + result);
9: },
10: error: function(){
11: alert("Error occured.");
12: }
13: });
但jQuery的ajax的data參數(shù)是變量寫成這樣就不行:
1: $.ajax({
2: type: "POST",
3: contentType: "application/json",
4: url: "./WebServices/MySampleService.asmx/GetServerResponse",
5: data:"{input:" + user + "}", //******* 此處錯(cuò),回調(diào)Error****************
6: dataType: 'json',
7: success: function(result) {
8: alert("jQuery callback: " + result);
9: },
10: error: function(){
11: alert("Error occured.");
12: }
13: });
最后繼續(xù)折騰加個(gè)單引號,寫成這樣data:'{"callerName":"'+user+'"}' 就沒問題了:
1: /// <reference src="jquery-1.3.2.js"/>
2:
3: var MyNameSpace = MyNameSpace || {};
4: MyNameSpace.Auth={};
5:
6: MyNameSpace.Auth.TestJQuery = function(user)
7: {
8: $.ajax({
9: type: "POST",
10: contentType: "application/json",
11: url: "./WebServices/MySampleService.asmx/GetServerResponse",
12: data:'{"callerName":"'+user+'"}', //注意格式!!"{callerName:abc}",可以,但 "{callerName:"+user+"}"不行,Error!
13: dataType: 'json',
14: success: function(result) {
15: alert("jQuery callback: " + result);
16: },
17: error: function(){
18: alert("Error occured.");
19: }
20: });
21:
22: $.ajax({
23: type: "POST",
24: contentType: "application/json",
25: url: "./WebServices/MySampleService.asmx/GetPersonList",
26: data: '{"input":"'+user+'"}', ////注意格式!"{input:56}",可以,但 "{input:"+user+"}"不行,Error!
27: dataType: 'json',
28: // beforeSend: function(){
29: // $("#tipsDiv").show();
30: // $("#tipsDivGT").text("Searching.... please wait");
31: // },
32: // error: function(){
33: // $("#tipsDivGT").text("Error occured!!");
34: // },
35: success: function(result) {
36:
37: //$('#dictionary').append(this.toString() + " ");
38: // alert(result.join(" | "));
39:
40: $(result).each(function() {
41: alert(this['ID'] + " " + this['Value']);
42: });
43:
44: }
45: });
46: }
47:
48: $(function() {
49: $("#btnTest").click(function() {
50: MyNameSpace.Auth.TestJQuery( $("#tbUserName").attr("value") );
51: });
52:
53: }
54:
55: );
立此存照希望對被同樣小問題困擾的同學(xué)有用!