WCF 服务接受简单的 JSON 字符串,但拒绝带有 400 错误的 JSON 集

发布于 2025-01-08 02:33:21 字数 2508 浏览 0 评论 0原文

我目前正在开发一个 PhoneGap 应用程序,它通过 WCF Web 服务与我们的数据库进行通信。

我向服务器发出了很多请求并毫无问题地检索了数据,但在每种情况下,我仅将几个参数作为我手动创建的 JSON 字符串传递(无字符串化)。现在,我在尝试从本地 SQLite 数据库读取数据然后将信息以 JSON 形式发送到 Web 服务时遇到了问题。该服务返回 400 错误,内容如下:

反序列化操作“uploadData”的请求消息正文时出错。 OperationFormatter 遇到无效的消息正文。期望找到名称为“type”、值为“object”的属性。找到值“数组”。

我按如下方式对 JSON 进行编码:

database.transaction(function(tx) {
    var query = "SELECT * FROM someTable WHERE something=something";
    tx.executeSql(query, [], function(tx, results) {
        var resultSet = new Array();

        for (i=0; i<results.rows.length; i++) {
            var row = results.rows.item(i);
            resultSet[i] = row;         
        }

        var json = JSON.stringify(resultSet);
    }
}

然后,我使用 jQuery 通过 ajax 请求发送 JSON 字符串:

$.ajax({
            type: "POST",
            url: "http://someurl/myService.svc/uploadData",
            contentType: "application/json; charset=utf-8",
            data: json,
            dataType: "json",
            success: function (data) {/*do something*/}, 
            error: function(jqXHR, textStatus, errorThrown) {
                $('#test').html(textStatus + " - " + errorThrown + " - " + jqXHR.responseText);
            }
        }); 

我的 Web 服务只是不断返回相同的错误。我的问题是这样...我如何从本地数据库读取并将结果集转换为可用的 JSON 字符串以通过 ajax 发送。

提前致谢。如果需要更多信息或问题不清楚,请告诉我,我会尽力提供更多信息。

编辑:尝试删除 stringify 但仍然收到 400 错误。不过,这次的错误有点不同:

操作“uploadData”的请求消息正文反序列化时出错。 OperationFormatter 遇到无效的消息正文。遇到意外的字符“u”。

编辑:我发现即使手动创建的完全有效的 JSON 字符串仍然会导致错误。当发送几个参数时,不会发生这种情况,例如:

{"name" : "dean", "age" : 23}

它工作得很好。它只是拒绝结果集,例如:

[{"name" : "dean", "age" : 23},{"name" : "bob", "age" : 25}]

该方法在 WCF 接口中定义如下:

 [OperationContract]
 [WebInvoke(Method = "POST",
          BodyStyle = WebMessageBodyStyle.Wrapped,
          ResponseFormat = WebMessageFormat.Json,
          RequestFormat = WebMessageFormat.Json)]

 string uploadData(string data);

看起来这更多是 WCF 服务的问题,而不是 JSON 字符串本身的问题。 有谁知道为什么 WCF 会接受一个简单的 JSON 字符串但拒绝一组?这是一个严重问题,已经完全停止了该项目的开发,因此非常感谢任何帮助!

已解决:我没有直接传递编码的 JSON 字符串,而是将其作为另一个带有“data”键的 JSON 字符串的值发送。因此,如上所述,我的 SQLite 结果如下:

var json = JSON.stringify(resultSet);

然后我将其传递给 WCF 服务,如下所示:

...
data: JSON.stringify({ data : json }),
...

现在它工作正常。

I'm currently working on a PhoneGap app which communicates with our database via a WCF web service.

I have made many requests to the server and retrieved data with no problem but in each case i was only passing in a couple of parameters as a JSON string which I created manually (no stringify). Now I have encountered a problem when trying to read from my local SQLite database and then send the information as JSON to the web service. The service is returning a 400 error with the following:

Error in deserializing body of request message for operation 'uploadData'. OperationFormatter encountered an invalid Message body. Expected to find an attribute with name 'type' and value 'object'. Found value 'array'.

I am encoding the JSON as follows:

database.transaction(function(tx) {
    var query = "SELECT * FROM someTable WHERE something=something";
    tx.executeSql(query, [], function(tx, results) {
        var resultSet = new Array();

        for (i=0; i<results.rows.length; i++) {
            var row = results.rows.item(i);
            resultSet[i] = row;         
        }

        var json = JSON.stringify(resultSet);
    }
}

I then send the JSON string via an ajax request using jQuery:

$.ajax({
            type: "POST",
            url: "http://someurl/myService.svc/uploadData",
            contentType: "application/json; charset=utf-8",
            data: json,
            dataType: "json",
            success: function (data) {/*do something*/}, 
            error: function(jqXHR, textStatus, errorThrown) {
                $('#test').html(textStatus + " - " + errorThrown + " - " + jqXHR.responseText);
            }
        }); 

My web service just keeps throwing back the same error. My question is this...How can I read from the local database and convert the result set into a usable JSON string to be sent via ajax.

Thanks in advance. If more info needed or the problem is unclear please let me know and I'll do my best to provide more information.

EDIT: Tried removing the stringify but I'm still getting a 400 error. The error is a little different this time though:

Error in deserializing body of request message for operation 'uploadData'. OperationFormatter encountered an invalid Message body. Encountered unexpected character 'u'.

EDIT: I've discovered that even a manually created JSON string that is perfectly valid still causes the error. It doesn't happen when sending a couple of parameters such as:

{"name" : "dean", "age" : 23}

It works perfectly fine. It's only rejecting sets of results such as:

[{"name" : "dean", "age" : 23},{"name" : "bob", "age" : 25}]

The method is defined in WCF interface as follows:

 [OperationContract]
 [WebInvoke(Method = "POST",
          BodyStyle = WebMessageBodyStyle.Wrapped,
          ResponseFormat = WebMessageFormat.Json,
          RequestFormat = WebMessageFormat.Json)]

 string uploadData(string data);

It seems it's more of a problem with the WCF service than the JSON string itself.
Does anyone know why WCF would accept a simple JSON string but reject a set? This is a major problem which has completely halted the development of the project so any help is greatly appreciated!

SOLVED: Instead of passing the encoded JSON string directly I instead sent is as a value of another JSON string with key "data". So, as above I have my SQLite results as:

var json = JSON.stringify(resultSet);

I then pass it to the WCF service as follows:

...
data: JSON.stringify({ data : json }),
...

And it now works fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆梦 2025-01-15 02:33:21

JSON.stringify 步骤是不必要的,因为 jQuery 的 AJAX 引擎会自动执行此操作。只需将结果集数组作为请求选项中的 data 属性传递即可:

...
data: resultSet,
...

您现在要做的是将数组字符串化为 JSON,然后要求 jQuery 在发送之前将该结果字符串重新字符串化为 JSON到服务器。

The JSON.stringify step is unnecessary because jQuery's AJAX engine does that automatically. Just pass in the result set array as the data property in the request options:

...
data: resultSet,
...

What you're doing now is stringifying an array to JSON, then asking jQuery to-restringify that resulting string as JSON before sending it to the server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文