我正在尝试使用ajax调用来调用我web服务,但是我不能成功地使用它调用web服务,请参阅以下代码
我正在尝试调用的Web方法
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Collections.Generic;
/// <summary>
/// Summary description for Jasonexample
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class Jasonexample : System.Web.Services.WebService {
public Jasonexample () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld(Dictionary<string,object>Input) {
return "Hello World " + Input["FName"].ToString() + " " + Input["LName"].ToString();
}
[WebMethod]
public string test ( )
{
return "test";
}
}我的ajax调用javascript
/// <reference path="jquery-1.2.3.min.js" />
$(document).ready(function() {
dict = new Object();
dict["FName"] = "Abhishek";
dict["LName"] = "Hingu";
$.ajax({
type: "POST",
url: "Jasonexample.asmx/HelloWorld",
data: '[{"Key":"FName","Value":"Abhi"},{"Key":"LName","Value":"Hingu"}]',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert("Hello");
alert(msg);
},
fail:function(msg)
{
alert("fail");
}
});
});现在,谁能告诉我如何在ajax中传递名为
发布于 2009-07-27 06:05:34
我用这个解决方案解决了这个问题(我使用的是Microsoft Ajax)
在服务器端的made方法:
[WebMethod]
public string GetPersonsList ( int page, string filter, int ID, OrderedDictionary SelectParameters )
{
//code
return outData;
}客户端调用使用这样的POST参数发出请求:
{"page":0,"filter":"","ID":-1,"SelectParameters":{"pClassId":1}}所以在你的调用中,你应该像这样设置数据变量:
data: '{"Input":{"FName":"Abhi","LName":"Hingu"}}'发布于 2009-07-28 02:25:14
要弄清楚特定集合如何进行反序列化,一个好方法是从测试方法返回该集合,并检查JavaScriptSerializer是如何序列化它的。例如,此方法:
[WebMethod]
public Dictionary<string,object> HelloWorld() {
Dictionary<string, object> d = new Dictionary<string, object>();
d.Add("FName", "Dave");
d.Add("LName", "Ward");
return d;
}返回此JSON:
{"FName":"Dave","LName":"Ward"}尝试将其用作$.ajax()数据参数:
data: "{'FName':'Dave','LName':'Ward'}"为了避免使用模糊集合带来的歧义,您可以考虑使用using a DTO class instead。
https://stackoverflow.com/questions/1186583
复制相似问题