0

I am trying to get the json file using $.getJSON by using:

$.getJSON('json/resume.json', function(data){
    alert('success');
});

but the alert message is not coming, i tried by:

$.ajax({
    type: 'POST',
    url: 'json/download.php',
}).success(function(){alert("hello")});

in this case the alert is coming as HELLO.

I am totally stuck. please help

Thanks

4

4 回答 4

2

You are using two different requests to two different urls and comparing them as if the same, the first is get to json/resume.json and the second is POST to json/download.php. The only reason the first call would fail is either one of these:

json/resume.json does not exist
json/resume.json does not contain valid json

You need to either set global ajax error handlers so that you will get your error from getJSON, or run the same json query through an ajax like:

$.ajax({
    url: 'json/resume.json',
    type: 'GET',
    dataType: 'json'
    success: function(response) {
        console.log(response)//should come into console anyway 
    },
    error: function(request, type, errorThrown) {

        message = (type=='parseerror') ? "json is invalid" : "(" + request.status + " " + request.statusText + ").";
        alert("error with request: "+message);
    }
})
于 2012-08-18T10:27:17.740 回答
0

the first is a GET, the second is a POST - did u use dev tools in browser and check the NET tab for server 500 errors?

if your server method is only accepting POSTs that's your bother right there.

于 2012-08-18T10:24:46.633 回答
0

Your server side script doesn't return valid JSON or doesn't set the Content-Type response header to application/json.

So make sure those 2 conditions are met before attempting to consume your script with $.getJSON:

<?php
    header('Content-Type: application/json');
    $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
    echo json_encode($arr);
?>
于 2012-08-18T10:26:10.513 回答
0

I think you should checkout error which happens in your getJSON(), e.g.

$.getJSON('json/resume.json', function(data){
    alert('success');
}).error(function(e, m) { alert('error'); console.log(m); });
于 2012-08-18T10:53:21.287 回答