Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.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
Javascript 对php的Ajax调用-未定义索引错误_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 对php的Ajax调用-未定义索引错误

Javascript 对php的Ajax调用-未定义索引错误,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,流程:主页->php上的ajax请求(tester.php)->返回主页的json信息 我找不到解决这个错误的办法。谢谢 ajax调用 $.ajax({ url : "tester.php", type : "POST", //dataType : 'json', data : { 'lat':lat,

流程:主页->php上的ajax请求(tester.php)->返回主页的json信息

我找不到解决这个错误的办法。谢谢

ajax调用

$.ajax({
                url : "tester.php",
                type : "POST",
                //dataType : 'json',
                data : {
                    'lat':lat,
                    'lng':lng,
                    'year1':year1,
                    'month1':month1,
                    'day1':day1,
                    'year2':year2,
                    'month2':month2,
                    'day2':day2,
                    'category':category
                },
                 success: function(data)
                {
                    $.getJSON('tester.php',function(cost)
                        {
                            document.getElementById('userdensity').innerHTML = cost[0]+","+cost[1];
                            document.getElementById('advertising_cost').innerHTML = cost[2]+","+cost[3];
                        });
                });
Php:(tester.Php):

不确定错误在哪里。我过去做过这件事,而且进展顺利

解决方案:将成功更改为:

success: function(data)
                {
                    var info = $.parseJSON(data);
                            document.getElementById('userdensity').innerHTML = info[0]+","+info[1];
                            document.getElementById('advertising_cost').innerHTML = info[2]+","+info[3];
                }

您试图使用getJson获取的信息已经由AJAX调用返回,因此您只需从返回的
数据
变量中检索即可。您可以重写为以下内容:

 $.ajax({
            url : "tester.php",
            type : "POST",
            //dataType : 'json',
            data : {
                'lat':lat,
                'lng':lng,
                'year1':year1,
                'month1':month1,
                'day1':day1,
                'year2':year2,
                'month2':month2,
                'day2':day2,
                'category':category
            },
             success: function(data){    
                response = $.parseJSON(data);                                       
                document.getElementById('userdensity').innerHTML = response[0]+","+response[1];
                document.getElementById('advertising_cost').innerHTML = response[2]+","+response[3];
            };
        });

在success参数中,您为什么再次调用$getJSON?您的
success
函数缺少一个关闭
}
是的,发生错误是因为您正在使用$.getJSON,它正在向tester.php发送GET请求。在这第二个不必要的请求上没有可用的POST变量,而不是调用
$。getJSON
使用
data
变量并将其解析为JSON。这修复了未定义的索引,但是data[0]='[',data[1]='''。你知道为什么吗?你能在成功回调中放入
console.log(data);
43.66203460000001“,”-79.425031999999“,“2015”,“2015”]数据[0]==[,数据[1]==”,数据[2]==4,数据[3]==3。如何使用字符串代替字符
success: function(data)
                {
                    var info = $.parseJSON(data);
                            document.getElementById('userdensity').innerHTML = info[0]+","+info[1];
                            document.getElementById('advertising_cost').innerHTML = info[2]+","+info[3];
                }
 $.ajax({
            url : "tester.php",
            type : "POST",
            //dataType : 'json',
            data : {
                'lat':lat,
                'lng':lng,
                'year1':year1,
                'month1':month1,
                'day1':day1,
                'year2':year2,
                'month2':month2,
                'day2':day2,
                'category':category
            },
             success: function(data){    
                response = $.parseJSON(data);                                       
                document.getElementById('userdensity').innerHTML = response[0]+","+response[1];
                document.getElementById('advertising_cost').innerHTML = response[2]+","+response[3];
            };
        });