views:

22

answers:

1

Hi,

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

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.

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

[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.

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)

+1  A: 

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.

See http://encosia.com/2009/04/07/using-complex-types-to-make-calling-services-less-complex/ for examples and a full discussion.

DaveB