Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax 不把我的json数组传给php,我不知道为什么_Ajax_Json_Simple Html Dom - Fatal编程技术网

Ajax 不把我的json数组传给php,我不知道为什么

Ajax 不把我的json数组传给php,我不知道为什么,ajax,json,simple-html-dom,Ajax,Json,Simple Html Dom,我的代码无法在浏览器中运行,并且无法将数据发送到process.php。我不知道问题出在哪里 <html> <head> <script src="//code.jquery.com/jquery-1.11.3.min.js"></script> <script type="text/javascript" src="json2.js"> </script> <script src="data-today.php" t

我的代码无法在浏览器中运行,并且无法将数据发送到process.php。我不知道问题出在哪里

<html>
<head>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="json2.js">
</script>
<script src="data-today.php" type="text/javascript" language="javascript"></script>


</head>
<body>


<script language="javascript" type="text/javascript">
<!--






$(document).ready(function () {

        var json = JSON.stringify(football);
        for (var i = 0; i < json.length; i++) {



alert(json);
$.ajax ({
  type:"POST",
  url:"process.php",
  contentType: "application/json",
  dataType: "json",
  async: true,
  data: {
         country: $("json[i][0]"),
         competition: $("json[i][1]"),
         team: $("json[i][2]")},
  success: function(){ alert("data")},
  error: function(){ alert("error")}
});
            }
    });







</script>


<table>
</table>
</body>
</html>
我原来的js数据结构-data-today.php(每天更新,我在head之后打开)

这是我的代码,我想将所有数据传递给一个简单的HTMLDOM解析器。我不知道问题出在哪里

<html>
<head>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="json2.js">
</script>
<script src="data-today.php" type="text/javascript" language="javascript"></script>


</head>
<body>


<script language="javascript" type="text/javascript">
<!--






$(document).ready(function () {

        var json = JSON.stringify(football);
        for (var i = 0; i < json.length; i++) {



alert(json);
$.ajax ({
  type:"POST",
  url:"process.php",
  contentType: "application/json",
  dataType: "json",
  async: true,
  data: {
         country: $("json[i][0]"),
         competition: $("json[i][1]"),
         team: $("json[i][2]")},
  success: function(){ alert("data")},
  error: function(){ alert("error")}
});
            }
    });







</script>


<table>
</table>
</body>
</html>


您已经将数据很好地保存在javascript数组中,如果您真的想将数据发布到for循环中,您可以使用如下内容

$(document).ready(function () 
{
    // scrap that line, you have the values in array (of arrays)
    //var json = JSON.stringify(football);

    for (var i = 0; i < football.length; i++) 
    {
        //alert(json);
        $.ajax ({
          type:"POST",
          url:"process.php",
          contentType: "application/json",
          dataType: "json",
          async: true,
          data: {
             country: football[i][0],
             competition: football[i][1],
             team: football[i][2],
          success: function(){ alert("data")},
          error: function(){ alert("error")}
        });
     }
});
$(文档).ready(函数()
{
//废掉那一行,你就有了数组中的值
//var json=json.stringify(足球);
对于(变量i=0;i
您可能还应该将data-today.php重命名为data-today.js,因为它似乎是一个javascript文件,而不是php文件…

(1)
var football
是一个数组,所以当您执行
var json=json.stringify(football);
时,它会将其转换为字符串,当您执行
for(var i=0;i
它在字符串字符上循环,而不是在数组上循环

(2) 如果
json
是一个数组,则无需将其设置为对象-
$(“json[i][0]”)
。只需直接访问它-
json[i][0]

所以现在你的代码应该是

<script language="javascript" type="text/javascript">

var football = new Array(); 
football[0] = new Array('Argentina','Primera Division','Boca Juniors'); 
football[1] = new Array('Argentina','Primera Division','Lanús'); 
football[2] = new Array('England','Premier League','Arsenal'); 
football[3] = new Array('England','Premier League','Liverpool');

$(document).ready(function () {

    var json = football;
    for (var i = 0; i < json.length; i++) {

        $.ajax({
          type:"POST",
          url:"process.php",
          contentType: "application/json",
          dataType: "json",
          async: true,
          data: {
              country: json[i][0],
              competition: json[i][1],
              team: json[i][2]},
          success: function(){ alert("data")},
          error: function(){ alert("error")}
        });
    }
});

</script>

var football=新数组();
足球[0]=新阵型(“阿根廷队”、“普雷米拉赛区”、“博卡青年队”);
足球[1]=新阵型(‘阿根廷’、‘普雷米拉赛区’、‘兰斯’);
足球[2]=新阵型(‘英格兰’、‘英超’、‘阿森纳’);
足球[3]=新阵型(‘英格兰’、‘英超’、‘利物浦’);
$(文档).ready(函数(){
var json=足球;
for(var i=0;i

JSFIDLE示例-(我注释掉了
error:function(){alert(“error”)}
中的
alert(“error”)
,但是您可以在Firebug Lite中展开
POST
,以查看
$.ajax()
posts。)谢谢大家的回答。如果我试图更新我的代码,我在firefox控制台上遇到了这个错误:SyntaxError:expected expression,在
标记之前的某个地方得到了“}”。我尝试了两个版本,但如果我尝试打开我的process.php,结果是一样的

my process.php:

    <?php
include ("auto3.php");
echo "<pre>";
    print_r($_POST);
echo "</pre>";
?>


$(“json[i][0]”)将字面上解析
$(“json[i][0]”)这是什么意思?如果
var football
是一个数组,为什么要使用
json.stringify()
然后尝试在
json[i][0]中作为数组访问它
?我已经尝试使用var json=football;但结果是same@Liverpool:这意味着它将逐字搜索所有html标记
,这并不是您想要做的。简而言之,您的“json[i][0]”既不解析[i]也不解析[0],因为它位于字符串中,在javascript中,必须连接。正确的格式仅为json[i][0],不带“”,但它在任何情况下都不起作用,因为这背后的逻辑是错误的,这是主要问题。此外,您应该将ajax请求排队,这一点很重要!我更新了代码,但在process.php中,我得到了空数组,结果是:Array()。如果我在最后启用了警报错误,那么我会在弹出窗口中得到简单的错误消息