Javascript Ping by Downloading JSON file
I'm trying to download a JSON file through javascript to use in a ping test but the browser appears to be interpreting it as javascript and gives parse errors. here is the code:
function sprawdz(adres)
{
//ping = 0;
startTime = new Date().getTime( );
$.ajax({
type: 'GET',
//url: 'http://'+adres+'/img/loading.gif',
url: 'URL TO JSON FILE HERE',
dataType: 'jsonp',
async: false,
setup: function() {
//
},
complete: function(xhr, text)
{
开发者_如何转开发 //alert(xhr.status + ' - ' + xhr.responseText + ' - ' + xhr.status);
finishTime = new Date( ).getTime( );
ping = finishTime - startTime;
//pngud(ping);
}
}
);
//return ping + ' ms';
}
You are specifying a dataType
of JSONP. JSONP is JavaScript.
Did you mean dataType: 'json'
?
Let's try to get a better idea of what the server is actually sending to you. Try this:
$.ajax({
type: 'GET',
url: '/json/ping',
dataType: 'text',
async: false,
success: function(data) {
console.log(data);
}
});
What shows up in the console?
its interpreted as json and should be a valid json string then. however what on earth is your definition of "ping"?
精彩评论