调用.NET Web服务上使用jQuery正试图将数据发布时,引起荣辱与共 [英] Calling .Net webservice with Jquery is causing woe when trying to post data

查看:126
本文介绍了调用.NET Web服务上使用jQuery正试图将数据发布时,引起荣辱与共的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code正确执行,当数据密钥没有数据要发送,即数据:{}一个空的JSON对象和web服务不带任何参数。我想张贴一些数据到web服务,但我遇到麻烦了。

The following code executes properly when the data key has no data to send, i.e. data: "{}" an empty JSON object and the webservice takes no parameters. I would like to post some data to the webservice but I am running into trouble.

当我尝试将它设置为数据:{'名':'尼尔','姓':'史密斯'},我得到一个错误

When I try to set this to data:"{'name':'Niall','surname':'Smith'}", I get an error

{"Message":"Invalid web service call, missing value for parameter: \u0027json\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"}

不执行的Web服务。

The webservice is not executed.

这是我的Jquery来电来邮我的数据返回给服务器。

This is my Jquery call to post my data back to the server.

    $.ajax({
        type: "POST",
        url: "/WebServices/BasketServices.asmx/AddItemToBasket",
        data: "{'name':'niall'}", // Is this Correct??
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnItemAddedSuccess
    });
function OnItemAddedSuccess(result,eventArgs) {
    //deserialize the JSON and use it to update the Mini Basket
    var response = JSON.parse(result.d);
}

这是我的WebService

here is my WebService

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class BasketServices : System.Web.Services.WebService
{
    [WebMethod(true)]
    public string AddItemToBasket(string json)
    {
       //do stuff
       return myString.toJSON();
    }
}

还有什么问题呢?难道是JSON数据的发布格式?难道是我没有设置我的web服务的正确的属性。怎么样的问题,戴夫·沃德的暗示帖子

我已经试过各种我能想到的。有没有人有什么想法?

I have tried everything I can think of. Does anyone have any ideas?

推荐答案

我觉得web服务期望参数 JSON 来进行设置。试试这个AJAX调用:

I think the webservice expects the parameter json to be set. Try this AJAX call :

var data = {'name':'niall'};

$.ajax({
    type: "POST",
    url: "/WebServices/BasketServices.asmx/AddItemToBasket",
    data: "json=" + JSON.stringify(data),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnItemAddedSuccess
});

其中, JSON.stringify()是一个方法,像在官方的实施发现:的 http://json.org/js.html

where JSON.stringify() is a method like the one found in the "official" implementation : http://json.org/js.html

这篇关于调用.NET Web服务上使用jQuery正试图将数据发布时,引起荣辱与共的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆