問題描述
我認為是一個相對簡單的 jQuery 插件有問題...
插件應該通過 ajax 從 php 腳本中獲取數據,以將選項添加到 <select>
.ajax 請求非常通用:
$.ajax({網址:o.url,類型:'發布',contentType: "application/x-www-form-urlencoded",數據:'{方法":getStates",程序":探索"}',成功:功能(數據,狀態){console.log("成功!!");控制臺.log(數據);控制臺.log(狀態);},錯誤:函數(xhr,desc,err){控制臺.log(xhr);console.log("Desc:" + desc + "
Err:" + err);}});
這似乎在 Safari 中運行良好.在 Firefox 3.5 中,服務器上的 REQUEST_TYPE
始終是 'OPTIONS',并且 $_POST 數據不會出現.Apache 將請求記錄為選項"類型:
::1 - - [08/Jul/2009:11:43:27 -0500] "OPTIONS sitecodes.php HTTP/1.1" 200 46
為什么這個 ajax 調用在 Safari 中有效,而在 Firefox 中無效,我該如何為 Firefox 修復它?
<上一頁>響應標頭日期:格林威治標準時間 2009 年 7 月 8 日星期三 21:22:17服務器:Apache/2.0.59 (Unix) PHP/5.2.6 DAV/2X-Powered-By: PHP/5.2.6內容長度 46保活超時=15,最大值=100連接保活內容類型 text/html請求標頭主機訂購單:8888用戶代理 Mozilla/5.0(Macintosh;U;Intel Mac OS X 10.5;en-US;rv:1.9.1) Gecko/20090624 Firefox/3.5接受 text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language en-us,en;q=0.5接受編碼 gzip,deflate接受字符集 ISO-8859-1,utf-8;q=0.7,*;q=0.7保活300連接保持活動來源 http://ux.inetu.act.org訪問控制請求方法 POSTAccess-Control-Request-Headers x-requested-with這是 Firebug 輸出的圖片:
報錯原因是同源策略.它只允許您對自己的域執行 XMLHTTPRequests.看看你是否可以使用 JSONP 回調代替:
$.getJSON('http://<url>/api.php?callback=?', function (data) { alert(data); });
Having trouble with what I thought was a relatively simple jQuery plugin...
The plugin should fetch data from a php script via ajax to add options to a <select>
. The ajax request is pretty generic:
$.ajax({
url: o.url,
type: 'post',
contentType: "application/x-www-form-urlencoded",
data: '{"method":"getStates", "program":"EXPLORE"}',
success: function (data, status) {
console.log("Success!!");
console.log(data);
console.log(status);
},
error: function (xhr, desc, err) {
console.log(xhr);
console.log("Desc: " + desc + "
Err:" + err);
}
});
This seems to work fine in Safari. In Firefox 3.5, the REQUEST_TYPE
on the server is always 'OPTIONS', and the $_POST data does not appear. Apache logs the request as type 'OPTIONS':
::1 - - [08/Jul/2009:11:43:27 -0500] "OPTIONS sitecodes.php HTTP/1.1" 200 46
Why would this ajax call work in Safari, but not Firefox, and how do I fix it for Firefox?
Response Headers Date: Wed, 08 Jul 2009 21:22:17 GMT Server:Apache/2.0.59 (Unix) PHP/5.2.6 DAV/2 X-Powered-By: PHP/5.2.6 Content-Length 46 Keep-Alive timeout=15, max=100 Connection Keep-Alive Content-Type text/html Request Headers Host orderform:8888 User-Agent Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5 Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language en-us,en;q=0.5 Accept-Encoding gzip,deflate Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive 300 Connection keep-alive Origin http://ux.inetu.act.org Access-Control-Request-Method POST Access-Control-Request-Headers x-requested-with
Here is a picture of the Firebug output:
The reason for the error is the same origin policy. It only allows you to do XMLHTTPRequests to your own domain. See if you can use a JSONP callback instead:
$.getJSON( 'http://<url>/api.php?callback=?', function ( data ) { alert ( data ); } );
這篇關于jQuery $.ajax(), $.post 發送“選項"作為 Firefox 中的 REQUEST_METHOD的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!