gpt4 book ai didi

javascript - 无法从本地主机文件获取 json 数据 - ajax、jquery

转载 作者:行者123 更新时间:2023-11-28 01:41:51 27 4
gpt4 key购买 nike

我当前的 php 项目文件夹中只有 json.txt,并且想将其内容传递给 javascript。昨天工作正常,但今天不行,浏览器以某种方式阻止它......

编辑:一切都在本地主机上,没有跨域。我附上了json内容,即使它是不相关的。也使用 Firefox 22 和一些 Opera 11,相同。对于 js,URL 为 127.0.0.1/aaa/index.php;对于 json,URL 为 127.0.0.1/aaa/json.txt

在此函数中,我获取数据但出现错误:calback。代码:

$(document).ready( function() {
$('#previous').click(function() {

$.ajax({
url: 'json.txt',
dataType: 'json',
//async: false,
cache: false,
success: function( data, status ){
alert('working');
alert( data.responseData.results.length + ' results found!' );
},
error: function(xhr, textStatus, err) {
alert('not working');
alert("readyState: "+xhr.readyState+"\n xhrStatus: "+xhr.status);//4, 200 ok
alert("responseText: "+xhr.responseText);//HERE IS CORRECT JSON CONTENT
}
});

});
});

//url in console [15:24:19.789] GET http://127.0.0.1/aaa/json.txt?_=1388413458433 [HTTP/1.1 200 OK 7ms]
//this ?_=1388413458433 keeps from some earlier attempt... ???

这是昨天有效的代码,但今天无效:

$(document).ready( function() {
$('#previous').click(function() {
$.getJSON("json.txt",function(data, status, xhr){

alert(status);//nothing
alert(JSON.stringify(data));
httpjson = data;
});

});
});

json.txt

{"post":{"glavni_search":"place","jumpMenu":"all","RadioGroup1":"mixed"},"status":"search successful.","result":{"count":30,"geo_count":1,"non_geo_count":29,"search_metadata":{"completed_in":0.057,"max_id":4.1747346982858e+17,"max_id_str":"417473469828583425","next_results":"?max_id=416844457594855423&q=place&result_type=mixed","query":"place","refresh_url":"?since_id=417473469828583425&q=place&result_type=mixed","count":15,"since_id":0,"since_id_str":"0"},"geo":[{"created_at":"Mon Dec 30 01:53:24 +0000 2013","id_str":"417473464963174400","text":"Im From A Place Were Them Jitts Play With Different Type Of Sticks.","user":{"id_str":"594862172","name":"R.I.P DAKOTA96'","screen_name":"Troublesome96_","profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000674448822\/1e32513fd6230ae6f161914ba8d0d884_normal.jpeg"},"coordinates":[25.9441399,-80.251417]}]}}

最佳答案

JSON 无效。 http://jsonlint.com/用它来调试 xhr 错误。

$.ajax({                    
url: 'jsonvalidan.txt',
dataType: 'json',
cache: false,
success: function( data, status ){
alert('radi');
alert(JSON.stringify(data));

alert( data.responseData.results.length + ' results found!' );
},
error: function(xhr, textStatus, err) { //odstampaj textStatus, err jbt
alert('ne radi');
alert(textStatus);
alert(err);
alert("readyState: "+xhr.readyState+"\n xhrStatus: "+xhr.status);
alert("responseText: "+xhr.responseText);
}
});

关于javascript - 无法从本地主机文件获取 json 数据 - ajax、jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20841672/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com