为什么不工作
问题描述:
这个jQuery AJAX调用C#web方法这里是我的JS:为什么不工作
function declassifyAjax(e) {
var items = getSelected();
var docIds = new Array();
items.each(get);
//get ids of QcItem/docId we are dealing with
function get(count, el) {
docIds[count] = $(el).parent().attr('id');
}
var dataObj = new Object();
dataObj.batchId = batchId;
dataObj.docIds = docIds;
var dataString = JSON.stringify(dataObj)
//make call to webservice to get html to recreate view showing
//pending declassification
$.ajax({
type: "POST",
url: applicationRoot + 'Models/BatchQC.asmx/declassify',
data: dataString,
contentType: "application/json; charset=utf-8",
success: function (data) {
if (ProcessWebMethodResult.processWebMethodResult(data) == true) {
declassifyProcess(data, e);
}
},
error: function (e) {
alert("Failed to Get declassification details");
}
});
}
这里是我的Web服务:
//type to represent the input the declassify method
public class DeclassifyType
{
public int batchId;
public string[] docIds;
}
[WebMethod(EnableSession = true)]
public WebMethodResult declassify(DeclassifyType dataString)
{
}
任何和所有帮助表示赞赏!
Firebug中的调试显示变量dataObj,batchId,docIds和dataString是正确的。我认为我的Web方法签名的设置方式有问题,因为Ajax从未被解雇。逐句通过.ajax方法时,它会发生错误,而不是成功。
答
您的Web方法需要一个参数,即您已拥有的数据对象,但是由于您直接传递对象而传递了多个参数。
相反,你需要有一个对象有一个属性,dataString
,并那属性的值应该是你的对象,像这样:
var dataString = JSON.stringify({ dataString: dataObj });
▲--should match--▼
public WebMethodResult declassify(DeclassifyType dataString)
答
啊,我只是固定它,
只是将签名更改为
[WebMethod(EnableSession = true)]
public WebMethodResult declassify(int batchId, string[] docIds)
{
}
真的很简单。感谢您检查我的帖子!
当你说“不工作”时,哪部分过程不起作用?你期望发生什么?什么错误信息,如果有的话,你会得到什么?您是否尝试过使用[Firebug](http://getfirebug.com/)或类似软件进行调试?请编辑您的问题并提供更多详细信息。 – 2010-10-21 09:57:48
'declassifyAjax'有没有解雇?如果你在里面放置一个alert()会发生什么? – 2010-10-21 10:06:06