1

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]}]}}
5
  • What URL are you calling it from? You may be running into some cross origin policy issues. Perhaps you're running everything on localhost and fetching the json over 127.0.0.1 or something? You may consider checking your developer console, you may find useful information there. Commented Dec 30, 2013 at 14:41
  • What is the content of json.txt? Are you sure it is valid JSON? Commented Dec 30, 2013 at 14:41
  • Have you are using IE? Commented Dec 30, 2013 at 14:45
  • Firefox 22. And see edited for json.txt content and crossdomain. URL's are 127.0.0.1/aaa/index.php and 127.0.0.1/aaa/json.txt Commented Dec 30, 2013 at 14:47
  • Entire callback isn't called , alert(status);//nothing also. Commented Dec 30, 2013 at 14:54

1 Answer 1

1

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);
    }
}); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.