Answer the question
In order to leave comments, you need to log in
Why does alert appear for a few seconds in Yandex, but not in other browsers?
Hello! There was no JSONP tag, so I just put JSON.
The problem is this:
There is a script that sends a request to my server in php using the JSONP protocol:
var CallbackRegistry = {};
function scriptRequest(url, onSuccess, onError) {
var scriptOk = false;
var callbackName = 'cb' + String(Math.random()).slice(-6);
url += ~url.indexOf('?') ? '&' : '?';
url += 'callback=CallbackRegistry.' + callbackName;
CallbackRegistry[callbackName] = function(data) {
scriptOk = true;
delete CallbackRegistry[callbackName];
onSuccess(data);
};
function checkCallback() {
if (scriptOk) return;
delete CallbackRegistry[callbackName];
onError(url);
}
var script = document.createElement('script');
script.onreadystatechange = function() {
if (this.readyState == 'complete' || this.readyState == 'loaded') {
this.onreadystatechange = null;
setTimeout(checkCallback, 0);
}
}
script.onload = script.onerror = checkCallback;
script.src = url;
document.body.appendChild(script);
}
function ok(data) {
alert( "Загружен время " + data.time );
}
function fail(url) {
alert( 'Ошибка при запросе ' + url );
}
scriptRequest("http://comet/php.php", ok, fail);
<?php
$d = new DateTime();
$response = json_encode(array(
time => $d->format("r")
));
print $_GET["callback"] . "(" . $response . ");";
?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question