开发者

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

Here is my 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:

//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, doc开发者_StackOverflow中文版Ids and dataString are correct. There is something wrong with the way my Web Method signature is setup I think, because the Ajax is never fired off. When stepping through the .ajax method, it goes to error, not success.


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:

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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜