3

I have want to do something like this to mimic a form post:

    $.ajax({
    url:url,
        type: "PUT",
    async:true,
    cache:false,
    data:
    {'end':end,'duration':duration}
    }

However, the data param only take data as a string, and the serialize() function only works if there is already a form.

Is there any way to parse the json array into a data string? (Multipart form)

Edison
  • 5,961
  • 4
  • 23
  • 39
  • Hi, you can take a look at the accepted answer here: http://stackoverflow.com/questions/191881/serializing-to-json-in-jquery – mamoo Oct 24 '11 at 06:37
  • Hi! Thanks for the fast response. Don't I need to put it as part of the request entity in the form format with separators and all? Thanks. – Edison Oct 24 '11 at 06:41

1 Answers1

2

Why do you think it only takes a string? Do you want to mimic an actual PUT that PUTs two variables, end and duration, or do you want to PUT a JSON string?

data Object, String
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

Here is a jsfiddle (using POST) that shows that it will work:
http://jsfiddle.net/Hqgbj/

And a screenshot of firebug:
http://cl.ly/253g1f3g032t1a381l0u

sberry
  • 128,281
  • 18
  • 138
  • 165
  • Thanks. I want to put it as multipart form data. – Edison Oct 24 '11 at 06:43
  • Thank you. It was actually a server error that my server code did not look for $_POST when PUT command is received. – Edison Oct 24 '11 at 06:49
  • 1
    In PHP, you can't read data for PUT out of `$_POST`. You need to read php://input. Here is an example of what you can do: `parse_str(file_get_contents("php://input"),$post_vars);` to dump the PUT data into an array called `$post_vars`. – sberry Oct 24 '11 at 06:53