简体   繁体   English

将JSON对象发布到Web方法 - $ .ajax

[英]Posting a JSON Object to a web method - $.ajax

I'm trying to get data from a form on a button click and send it to a web method in my code behind. 我正试图通过点击按钮从表单中获取数据,并将其发送到我后面的代码中的Web方法。 I'd like to pass it as a JSON object or at least I think that's the convention? 我想将它作为JSON对象传递,或者至少我认为这是惯例? Here's my current code however it generates an error (Shown below the code). 这是我当前的代码,但它会产生错误(在代码下方显示)。

$("#addTask")
    .click(function( event ) {
    var newTask = new Object();
        newTask.TaskName = $('#ctl00_ContentArea_taskName').val();
        newTask.TaskDescription = $('#ctl00_ContentArea_taskDescription').val();
        newTask.SQLObjectID = $('#ctl00_ContentArea_sqlReportingID').val();
        newTask.WarehouseSQLObjectID = $('#ctl00_ContentArea_warehouseSQLObjectID').val();

        $.ajax({
           type: "POST",
           url: 'AddTask.aspx/validateTask',
           data: JSON.stringify(newTask),
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           async: false,
           success : function(data) { 
                alert( data.d ); 
           }
        });  
    });

__ __

{"Message":"Invalid web service call, missing value for parameter: \u0027newTask\u0027.","StackTrace":"   at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

I've tried having my web method in a bunch of different ways, such as but not limited to: 我已尝试以多种方式使用我的网络方法,例如但不限于:

<System.Web.Services.WebMethod()> _
Public Shared Function validateTask(ByVal newTask As TaskBO)

Or with a bunch of individual parameters as strings. 或者将一堆单独的参数作为字符串。

What is the correct way to accomplish what I'm trying to do? 完成我想要做的事情的正确方法是什么? What do I not understand about formatting JSON objects? 关于格式化JSON对象我怎么理解?

Thanks for your help! 谢谢你的帮助!

The web service expects an item with the key "newTask" (as shown by your method's parameter). Web服务需要一个带有“newTask”键的项(如方法参数所示)。 Your request will be sent as: 您的请求将发送至:

{
    "TaskName": "stuff",
    "TaskDescription": "stuff",
    "SQLObjectID": "stuff",
    "WarehouseSQLObjectID": "stuff"
}

But you really need it to be: 但你真的需要它:

{
    "newTask": {
        "TaskName": "stuff",
        "TaskDescription": "stuff",
        "SQLObjectID": "stuff",
        "WarehouseSQLObjectID": "stuff"
    }
}

So change your $.ajax() call to be: 因此,将$.ajax()调用更改为:

data: JSON.stringify({
    newTask: newTask
}),
contentType: "application/json; charset=utf-8",
  • If you are using a separate js file for this code to work, verify the client ids 如果您使用单独的js文件来使用此代码,请验证客户端ID

  • If there are more properties on the class for the newTask , you can always exclude unnecessary properties from json object using delete operator.( for example: delete newTask.UnnecessaryProperty) 如果newTask的类上有更多属性,则始终可以使用delete运算符从json对象中排除不必要的属性。(例如:delete newTask.UnnecessaryProperty)

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

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