如何使用jQuery的ajax方法将json数组发布到PHP? [英] How to post json array to PHP using jQuery's ajax method?

查看:101
本文介绍了如何使用jQuery的ajax方法将json数组发布到PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用javascript创建的json数组,需要将其发布到php文件中才能将数据保存到数据库中.

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.
       ;           

我尝试使用jQuery.ajax发布它,但未成功.

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.

我使用以下错误:

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

[对象对象] -parsererror-SyntaxError:JSON.parse:意外字符

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

推荐答案

尝试将data: myArray更改为data: {mydata : myArray}.直接传递数组会导致jQuery以奇数格式传递它. http://api.jquery.com/jQuery.param/

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/

这篇关于如何使用jQuery的ajax方法将json数组发布到PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆