Dave-88
Dave-88

Reputation: 227

what is the right syntax for a JSON encoded PUT request using jQuery AJAX

I write my ajax PUT request:

jQuery.ajax({
                    url: url_lab_data+"backend/read/{\"f_anid\":"+anid+"}",
                    type: "PUT",
                    data: JSON.stringify({"read": 1}),
                    contentType: "application/json",
                    success: function (data) {

                    }
                });

But I get an OPTION Method in the Network log in Chrome. Why?

Is the Syntax not correct?

I hope someone can help me.

Upvotes: 4

Views: 15829

Answers (1)

Pawan Pillai
Pawan Pillai

Reputation: 2065

This is what I do. Hope it helps:

var WebServiceUrl = 'SomeWebservice.asmx/SomeMethod';
var DataToSend = new Object();
DataToSend = {  
                FirstName : 'John',
                LastName : 'Smith'
             };

//Call jQuery ajax
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: WebServiceUrl,
    data: JSON.stringify(DataToSend),
    dataType: "json",
    success: function (msg) {
        alert('Success');
    },
    error: function (err){
        alert('Error');
    }
});

And assuming you have a Webservice. I have a simple ASP.NET VB web service: SomeWebservice.asmx and its method signature as follows:

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function SomeMethod(ByVal FirstName As String, ByVal LastName As String) As String

Upvotes: 5

Related Questions