64

I am trying to put some JSON formatted data via Ajax with jQuery to a server. My code looks like this:

$.ajax({
    type: "PUT",
    url: myURL,
    contentType: "application/json",
    data: {"data": "mydata"}
});

But on the server-side, I receive a data=mydata string, instead of the expected JSON. Firebug tells me the same.

Where is the error?

4
  • What are you using on the server-side? Commented Nov 17, 2009 at 14:37
  • I use Couchdb which expects JSON. Commented Nov 17, 2009 at 14:43
  • 6
    AJAX PUT is available in all major browser. HTTP PUT isn't. So in this case, it's fine (recommended, even) to use PUT. Commented Jul 2, 2013 at 19:39
  • 2
    Adding the option dataType: "json", might help you out here. Commented Dec 29, 2013 at 14:05

3 Answers 3

55

I think the data needs to be a String. Objects are converted to query strings which is what you are seeing here.

You can use the JSON.stringify(obj) method to convert your Object to a String. The code for the JSON object is available from: https://github.com/douglascrockford/JSON-js/blob/master/json2.js.

Alternately, just pass the code you are using to create the object as a literal String, but I imagine this is just an example and you'll want to encode some object you've already created.

Sign up to request clarification or add additional context in comments.

2 Comments

This is the way @Juri should go if he wants JSON on the server. I use the json2.js library all the time and it works great.
would be nice if jQuery would stringify the data, just as it does with POST.
33

If you always have to send JSON in your application, then you can just execute this somewhere in your init and then use default $.ajax call as in your example, and it will always serialize as a JSON string instead of the Ajax default query string.

Here I use the JSON object mentioned above:

$.ajaxSetup({
    contentType : 'application/json',
    processData : false
});
$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
    if (options.data){
        options.data=JSON.stringify(options.data);
    }
});

Comments

1
//url: this is a reference to the XML, where you need to define the mapping.
//<entry key="/getEmpDetails/transEfileGenerate.app">
//<bean class="com.adp.ems.framework.spring.MappingItem" p:delegate-ref="efilePageDelegate"
//p:action="passJSONObjectAndGetWebServiceOutput" />

//str1 is the input JSON that you need to pass... Ajax will automatically take care to get the response.
//</entry>

var kw = {
    url : "getEmpDetails/transEfileGenerate.app",
    timeout : 30000,
    handleAs : "json",
    sync: false,
    putData : str1,
    headers: { "Content-Type": "application/json"},
    load : function(result) {
},

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.