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

20,962

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.

Removed dangerous link here

Share:
20,962
Morgan
Author by

Morgan

Updated on April 11, 2020

Comments

  • Morgan
    Morgan about 4 years

    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)

    • Samo
      Samo over 13 years
      What exactly "fails saying 'error'"? Is that from your callback function (which should be returning "fail" + status and not "error" so I'm confused)? In other words, what is the error message and where does it come from?
    • Morgan
      Morgan over 13 years
      It's: error: function (xhr, status) { alert("fail: " + status); and the status message is just 'error'. I am confused too, first time I'm doing this heh.
  • Erik Anderson
    Erik Anderson about 7 years
    Google Chrome flagged encosia.com as dangerous: The site ahead contains malware - Attackers currently on encosia.com might attempt to install dangerous programs on your computer that steal or delete your information (for example, photos, passwords, messages, and credit cards).