无法从localhost文件获取json数据 - ajax,jquery

时间:2013-12-30 14:38:42

标签: javascript php jquery ajax json

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

编辑:一切都在localhost上,没有跨域。我附上了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]}]}}

1 个答案:

答案 0 :(得分:1)

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);
    }
});