简体   繁体   English

为什么此jQuery Ajax调用C#Web方法不起作用

[英]Why is this jQuery ajax call to C# web method not working

Here is my JS: 这是我的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");
        }
    });
}

And here is my Web Service: 这是我的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)
    {
    }

Any and all help appreciated! 任何和所有帮助表示赞赏!

Debugging in Firebug shows that the variables dataObj, batchId, docIds and dataString are correct. 在Firebug中进行调试表明变量dataObj,batchId,docIds和dataString是正确的。 There is something wrong with the way my Web Method signature is setup I think, because the Ajax is never fired off. 我认为Web方法签名的设置方式存在问题,因为Ajax从未触发过。 When stepping through the .ajax method, it goes to error, not success. 单步执行.ajax方法时,它会出错,而不是成功。

Your web methods expects one parameter, the data object you already have, but you're passing multiple parameters since you're passing the object directly. 您的网络方法需要一个参数,即您已经拥有的数据对象,但是您要传递多个参数,因为您是直接传递对象。

Instead, you need to have an object with one property, dataString , and that property's value should be your object, like this: 相反,您需要一个具有一个属性dataString的对象,并且属性的值应该是您的对象,如下所示:

var dataString = JSON.stringify({ dataString: dataObj });
                                    ▲--should match--▼
public WebMethodResult declassify(DeclassifyType dataString)

Ah, I just fixed it, 啊,我刚修好,

just changed the signature to 刚刚将签名更改为

[WebMethod(EnableSession = true)]
public WebMethodResult declassify(int batchId, string[] docIds)
{
}

Simple really. 真的很简单。 Thanks for checking my post! 感谢您检查我的帖子!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM