jQuery 如何将 JSON 对象发送到 asp.net Web 服务并在那里处理数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4039634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:33:20  来源:igfitidea点击:

How to send JSON object to asp.net web service and process the data there?

asp.netjqueryajaxweb-services

提问by Morgan

I am using the local database in web kit browsers and to get the data from the database I have the following code:

我在 web kit 浏览器中使用本地数据库并从数据库中获取数据,我有以下代码:

function synchronise() {
        myDB.transaction(
        function (transaction) {
            transaction.executeSql("SELECT * FROM Patients;", [], synchroniseHandler, errorHandler);
    }
);

With I am trying to do now with the synchroniseHandler is to send all the rows to a web service and process the data there.

我现在尝试使用同步处理器将所有行发送到 Web 服务并在那里处理数据。

function synchroniseHandler(transaction, results) {
    for (var i = 0; i < results.rows.length; i++) {
        var row = results.rows.item(i);
        var patient = new Object();

        patient.name = row['name']
    patient.address = row['address']
        patient.city = row['city']
    patient.state = row['state']
        patient.zip = row['zip']
    patient.phone = row['phone']

        $.ajax({
            type: "POST",
            url: "MyService.asmx/synchronise",
            data: JSON.stringify(patient),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
        success: function (msg) {
                alert("success");
            },
            error: function (xhr, status) {
                alert("fail" + status);
            }
        });
    }
}

However it always fails saying "error"

但是它总是无法说“错误”

It's an ASP.NET 2.0 webb application but I am using JSON.NET and my webmethod to get the data is

这是一个 ASP.NET 2.0 webb 应用程序,但我使用的是 JSON.NET,我的 webmethod 来获取数据是

[WebMethod]
        public void synchronise(string patient)
        {
        JObject o = JObject.Parse(patient);
                string name = (string)o["name"];
              string address = (string)o["address"];
        string city = (string)o["city"];
                string state = (string)o["state"];
                string zip = (string)o["zip"];
                string phone = (string)o["phone"];

As it is now I am not using ajax but have JavaScript function that gets all rows and then insert them in a remote database when I click a button and it works. However, I am tryint to insert these automatically without a postback.

就像现在一样,我没有使用 ajax,而是使用 JavaScript 函数来获取所有行,然后当我单击一个按钮时将它们插入到远程数据库中并且它可以工作。但是,我试图在没有回发的情况下自动插入这些。

Any suggestions on how I can get this to work?

关于如何使其工作的任何建议?

EDIT:Seems like the error comes from the web service:

编辑:似乎错误来自网络服务:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

System.InvalidOperationException: Request format is invalid: application/json; charset=UTF-8.

   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()

   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

edit2... added support for httppost in web.config and got rid of Failed to load resource: the server responded with a status of 500 (Internal Server Error)

edit2... 在 web.config 中添加了对 httppost 的支持并摆脱了无法加载资源:服务器响应状态为 500(内部服务器错误)

回答by DaveB

Your web service method is expecting a string as a parameter. You are passing in a complex type. Define a matching type on the server and use it as the parameter type. You shouldn't need to create a object from the JSON string as you are.

您的 Web 服务方法需要一个字符串作为参数。您传入的是复杂类型。在服务器上定义一个匹配类型并将其用作参数类型。您不需要像现在这样从 JSON 字符串创建对象。

Removed dangerous link here

在这里删除了危险链接