简体   繁体   中英

Can't get json data from localhost file - ajax, jquery

I just have json.txt in current php project folder, and want to pass it's content to javascript. Yesterday worked fine, but today it wont, browser prevent it somehow...

EDIT : Everything is on localhost, no cross domain. I attached json content, even if it is irrelevant. Also using Firefox 22, and some Opera 11, same. URL's are 127.0.0.1/aaa/index.php for js and 127.0.0.1/aaa/json.txt for json.

In this function I get data but in error: calback. Code:

$(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... ???

Here is yesterday code that worked, but not today:

$(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 was not valid. http://jsonlint.com/ Use this to debug xhr errors.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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