我想在'beforeunload'事件上對服務器進行JSONP調用。這一切都很好,直到我開始使用後退按鈕。如果我離開頁面,然後在下次調用beforeunload事件時按'返回',它看起來會生成JSONP請求,但服務器永遠不會收到它。JSONP + onbeforeunload +後退按鈕+問題
注意(1):已經在IE和FF上測試過(兩者都出現問題)。 (2):我也測試過使用jQuery.getJSON方法,它有同樣的問題,所以我認爲makeJSONPCall函數是正確的。 (我可能是錯的)
任何想法的人?
一些代碼方面:
JSONP CALL:
makeJSONPCall('http://serverurl.mvc/method?args=' + data, 'callbackfunction');
function makeJSONPCall(url, callbackname)
{
if (url.indexOf("?") > -1) url += "&jsonp="
else url += "?jsonp="
url += callbackname + "&";
url += new Date().getTime();
var script = document.createElement("script");
script.setAttribute("id", "JSONP");
script.setAttribute("src",url);
script.setAttribute("type","text/javascript");
if (document.head) document.head.appendChild(script);
else document.body.appendChild(script);
}
感謝
感謝您的及時迴應克里斯。不幸的是,作爲公用事業腳本,我不能在人們的網頁上發佈警報。我其實並不認爲這裏有解決方案,因爲在我收到服務器回覆之前,我無法在beforeunload事件中「等待」。 謝謝反正 – gatapia 2009-11-19 00:04:39