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

I use the local database in web bundle browsers and I have the following code to retrieve data from the database:

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

When I try to do this with synchronization, the Handler must send all the lines to the web service and process the data there.

 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, he always fails saying "mistake"

This is an ASP.NET 2.0 web application, but I use JSON.NET and my web method to get the data

 [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 of now, I do not use ajax, but I have a JavaScript function that receives all the rows and then inserts them into the remote database when I click the button and it works. However, I am trying to insert them automatically without postback.

Any suggestions on how I can make this work?

EDIT: It appears that the error is coming from a 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 the resource: the server responded with a status of 500 (Internal server error)

+2
jquery ajax web-services
source share
1 answer

Your web service method expects a string as a parameter. You go through a difficult type. Determine the appropriate type on the server and use it as the parameter type. You do not need to create an object from a JSON string as it is.

Removed dangerous link here

+4
source share

All Articles