尝试发布数据时,使用 Jquery 调用 .Net webservice 会导致麻烦 [英] Calling .Net webservice with Jquery is causing woe when trying to post data

查看:24
本文介绍了尝试发布数据时,使用 Jquery 调用 .Net webservice 会导致麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当数据键没有要发送的数据时,以下代码正确执行,即 data: "{}" 一个空的 JSON 对象并且网络服务不接受任何参数.我想将一些数据发布到网络服务,但遇到了麻烦.

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.

当我尝试将其设置为 data:"{'name':'Niall','surname':'Smith'}" 时,出现错误

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: u0027jsonu0027.","StackTrace":"   at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)
   at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)
   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)
   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

未执行网络服务.

这是我的 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(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数据的格式吗?可能是我没有在我的 WebService 上设置正确的属性.戴夫·沃德的帖子

What could the problem be? Is it the format of the JSON data to be posted? Could it be that I haven't set the correct Attributes on my WebService. What about the problems alluded to in Dave Ward's post

我已经尝试了所有我能想到的方法.有人有什么想法吗?

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

推荐答案

我认为网络服务需要设置参数 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

这篇关于尝试发布数据时,使用 Jquery 调用 .Net webservice 会导致麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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