Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.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 jQuery.getJSON()不工作_Javascript_Php_Jquery_Json - Fatal编程技术网

Javascript jQuery.getJSON()不工作

Javascript jQuery.getJSON()不工作,javascript,php,jquery,json,Javascript,Php,Jquery,Json,我试图从php脚本中获取JSON数组。以下是我在jsp文件中编写的Jquery代码- $(document).ready(function() { alert("Inside Ready"); $.getJSON('http://example.com/root_dir/test_json.php', function(data) {

我试图从php脚本中获取JSON数组。以下是我在jsp文件中编写的Jquery代码-

$(document).ready(function()
            {
                    alert("Inside Ready");
                    $.getJSON('http://example.com/root_dir/test_json.php', function(data)
                    {
                        alert(data); 
                    });

            });
但是,上面的代码只显示外部警报(即警报(“内部就绪”);而不显示内部警报(即警报(数据)))。当我在浏览器中点击URL时,我得到了预期的json。所以在URL和php脚本中肯定没有问题

下面是test_json.php

<?php

//Create an array
$json_response = array();

        $row_array['label'] = 'A';
        $row_array['value'] = $row['0 to 2'];
        $row_array['color'] = '#FA2020';

         array_push($json_response,$row_array);

        $row_array['label'] = 'B';
        $row_array['value'] = $row['2 to 3'];
        $row_array['color'] = '#2BD95A';

         array_push($json_response,$row_array);

        $row_array['label'] = 'C';
        $row_array['value'] = $row['above 3'];
        $row_array['color'] = '#F7F739';

        //push the values in the array
        array_push($json_response,$row_array);

   echo json_encode($json_response); 
?>

我正在使用jquery-1.10.2.js
。谢谢你

试试这个……希望对你有用

        $(document).ready(function()
        {
            $.ajax({
                    type:'POST',
                    url:'http://example.com/root_dir/test_json.php',
                    dataType:'JSON',
                    data:{
                    },
                    success:function(data1){
                            alert(data)
                    },
                    error:function(XMLHttpRequest,textStatus,errorThrown){
                        alert("error");
                    }

                });
       });

您的代码似乎运行良好- 我刚刚用你的代码创建了一个测试页面,它可以-

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
    alert("Inside Ready");
    $.getJSON('http://<<CHANGE SERVER NAME>>/zz-test/get_json.php', function(data)
    {
        alert(data);
    });
});
</script>
</head>
<body>
</body> 
</html>

$(文档).ready(函数()
{
警报(“内部就绪”);
$.getJSON('http:///zz-test/get_json.php,函数(数据)
{
警报(数据);
});
});

您的jQuery和PHP代码看起来很好,因此根据我的经验,这通常是从不同的域(即:file://)调用PHP脚本引起的错误。如果您可以访问浏览器的控制台,您应该能够看到这是否是导致数据无法显示的错误

一种解决方案是在PHP代码的顶部添加:
header('Access-Control-Allow-Origin:')。然而,这存在一些安全问题,因此不应永久使用

或者,您可以将所有HTML、CSS、JS、jQuery等代码上载到托管PHP文件的web服务器,这是一个更好的选择


最后,如果上面的选项不可行,您可以使用JSON-p(尽管这不适用于POST请求),请查看浏览器的开发人员工具。重新加载页面。JavaScript控制台说什么?您能在Net选项卡中看到您的Ajax请求吗?格式正确吗?它得到回应了吗?响应是否正确?您的PHP告诉浏览器它正在发送HTML,您应该有
标题(“内容类型:application/json”)在那里。在ajax调用中添加一个错误处理程序!我认为您的http请求不包含Access Control Allow Origin标头。请检查一下,我终于找到了答案。实际上存在“No Transport”错误,所以参考上面链接上的答案,我只是将“$.support.cors=true;”语句放在http请求之前。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
    alert("Inside Ready");
    $.getJSON('http://<<CHANGE SERVER NAME>>/zz-test/get_json.php', function(data)
    {
        alert(data);
    });
});
</script>
</head>
<body>
</body> 
</html>