Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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",
                "picture_comment":"some comment about picture1", 
                "pictureid":16, 
                "u0id":80, 
                "u1id":82,
                "u2id":78,
                "u3id":79,
                "u4id":81, 
                "param0": "60f3f",
                "param1":"48dd2",
                "param2":"4f2f7",
                "param3":"8d101",
                "param4":"5efcc",
                "active":false,
                "dutyid":1256,
                "t":0
               },
               {
                "picture":"picture2.jpg",
                "picture_comment":"some comment about picture2",
                "pictureid":160,
                "u0id":18,
                "u1id":48,
                "u2id":70,
                "u3id":95,
                "u4id":74,
                "param0": "1123f",
                "param1":"48d13",
                "param2":"595f7",
                "param3":"78ad8",
                "param4":"4efdc",
                "active":false,
                "dutyid":125,
                "t":4
               }
               //goes like this about more than 20 times.
       ;           

I have tried to post it using jQuery.ajax but I was not successful.

 $.ajax({
          type: "POST",
          url: "goDoMyWork.php",
          data: myArray,
                      dataType: "json",
          success: function(){

            alert("Done! You'll be redirecting now.");
            window.location.href = "/index.php";
        }
        ,
        error: function(jqXHR, textStatus, errorThrown)
        {
            alert("Fail!");
        }
    });

I have to insert some of the data into one table and some of them into another table.

I have got the following error using:

alert(jqXHR + '-' + textStatus + '-' + errorThrown);

[object Object]-parsererror-SyntaxError: JSON.parse: unexpected character

share|improve this question
 
What isn't successful? What error it gives? Did you receive data in PHP? –  Wagner Nov 18 '13 at 17:53
 
First, that isn't a json array, it's just an array. If you want it to be json, you must stringify it. Secondly, the error you are getting suggests that your php isn't returning json (json is the format you told jquery to expect by using dataType: "json") –  Kevin B Nov 18 '13 at 18:55
add comment

2 Answers

up vote 1 down vote accepted

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/

share|improve this answer
 
Well, that solved my problem. As @KevinB says, my array is not json. It is just string. I make a json array using your suggestion. Thanks. –  zkanoca Nov 18 '13 at 19:17
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.