Skip to content Skip to sidebar Skip to footer

How To Post Json Array To Php Using Jquery's Ajax Method?

I have a json array created by javascript and need to post it to a php file in order to save the data into the database. var myArray = [{ 'picture':'picture1.jpg',

Solution 1:

Try changing data: myArray to data: {mydata : myArray}. Passing your array directly causes jQuery to pass it in an odd format. http://api.jquery.com/jQuery.param/

Solution 2:

JSON.parse is the function that transforms text in json-format into a JavaScript object. What you seem to be wanting to do is throw some data at the server, where the server will handle it further.

What you are actually doing is setting the dataType to json. In the documentation you can read that this is the data-type you are expecting to get back from the server. jQuery will therefor throw the data it gets back from the server through JSON.parse, but apparently the data is not a well-formed json-string. Removing the dataType from your request will fix this, as the data will most likely not be parsed. Therefor, whatever gets returned will be in the form of a string.

Post a Comment for "How To Post Json Array To Php Using Jquery's Ajax Method?"