简体   繁体   English

Ajax“无法加载资源”

[英]Ajax “Failed to load resource”

jQuery('input').live('click',function(e){
    $.getJSON(
        "/json.php",
        function(data){
            the_name = data.name;
        }
    );

});

When we press , it should make a json query. 当我们按时,它应该进行json查询。

Bit it gives errors. 有点错误。

In Google Chrome console: 在Google Chrome控制台中:

In Firefox console: 在Firefox控制台中:

The strange is when I open http://site.com/json.php , browser gives me a normal json code like: {"name":"Mary"} . 奇怪的是,当我打开http://site.com/json.php时 ,浏览器为我提供了一个普通的json代码,例如: {"name":"Mary"} It is encoded with php json_encode(); 它使用php json_encode();编码json_encode();

What is the problem? 问题是什么?

maybe your json string is not correct: 也许您的json字符串不正确:

try 尝试

$.get("/json.php", function(data) {alert(data)});

if you see you data in the alert box try: 如果在警报框中看到数据,请尝试:

$.get("/json.php", function(data) {
  var obj = $.parseJSON(data);
  alert(obj.name)
});

您的json.php脚本未设置Content-Type: application/json HTTP标头?

Load up Firebug and check the request and response using the Console. 加载Firebug并使用控制台检查请求和响应。 Make sure the request is getting sent properly and that the response from the server is properly formatted JSON. 确保正确发送了请求,并且服务器的响应格式正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM