WCF service accepts plain JSON string but rejects JSON set with 400 error

I am currently working on a PhoneGap application that communicates with our database through the WCF web service.

I made a lot of requests to the server and received the data without problems, but in each case I only passed a couple of parameters as a JSON string, which I created manually (without lines). Now I ran into a problem when trying to read from my local SQLite database and then send this information as JSON to the web service. The service returns error 400 with the following:

Error deserializing the body of the request message for the operation 'uploadData'. OperationFormatter detected an invalid message body. It is expected to find an attribute with the name "type" and the value "object". Found value "array".

I encode 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); } } 

Then I send the JSON string through 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 throws the same error. My question is ... How can I read from a local database and convert the result set to a convenient JSON string to send via ajax.

Thanks in advance. If you need more information or if the problem is not clear, let me know and I will do my best to provide additional information.

EDIT: Tried to delete the line, but I still get the 400 error. The error this time is slightly different:

Error deserializing the body of the request message for the operation 'uploadData'. OperationFormatter detected an invalid message body. Unexpected u character detected.

EDIT: I found that even a manually created JSON string that is perfectly valid still causes an error. This does not happen when sending multiple parameters, such as:

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

It works great. It rejects many results, such as:

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

The method is defined in the WCF interface as follows:

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

This seems to be more of a problem with the WCF service than the JSON string itself. Does anyone know why WCF accepts a simple JSON string but rejects the set? This is a serious problem that completely stopped the development of the project, so any help is greatly appreciated!

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

 var json = JSON.stringify(resultSet); 

Then I pass it to the WCF service as follows:

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

And now it works great.

+6
source share
1 answer

The JSON.stringify step JSON.stringify not needed because the jQuery AJAX engine does this automatically. Just pass an array of the result set as the data property in the query parameters:

 ... data: resultSet, ... 

Now you create an array for JSON, and then query jQuery to restore the resulting string as JSON before sending it to the server.

-1
source

Source: https://habr.com/ru/post/909110/


All Articles