遇到麻煩我認爲是一個相對簡單的jQuery插件... php
該插件應該經過ajax從php腳本中獲取數據,以向<select>
添加選項。 ajax請求很是通用: html
$.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 + "\nErr:" + err); } });
這彷佛在Safari中運行良好。 在Firefox 3.5中,服務器上的REQUEST_TYPE
始終爲「OPTIONS」,而且不顯示$ _POST數據。 Apache將請求記錄爲「OPTIONS」類型: ajax
::1 - - [08/Jul/2009:11:43:27 -0500] "OPTIONS sitecodes.php HTTP/1.1" 200 46
爲何這個ajax調用在Safari中工做,而不是Firefox,以及如何爲Firefox修復它? json
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
這是Firebug輸出的圖片: api
我彷佛若是o.url = 'index.php'
而且該文件存在,則在控制檯中返回成功消息。 若是我使用url: http://www.google.com
: http://www.google.com
,則會返回錯誤 服務器
若是發帖子請求爲何不直接使用$ .post方法: app
$.post("test.php", { func: "getNameAndTime" }, function(data){ alert(data.name); // John console.log(data.time); // 2pm }, "json");
它簡單得多。 post
你能夠試試嗎? google
contentType:application/x-www-form-urlencoded
url
錯誤的緣由是相同的原始策略。 它只容許您對本身的域執行XMLHTTPRequests。 看看你是否能夠使用JSONP回調:
$.getJSON( 'http://<url>/api.php?callback=?', function ( data ) { alert ( data ); } );
嘗試添加選項:
dataType:「json」
function test_success(page,name,id,divname,str) { var dropdownIndex = document.getElementById(name).selectedIndex; var dropdownValue = document.getElementById(name)[dropdownIndex].value; var params='&'+id+'='+dropdownValue+'&'+str; //makerequest_sp(url, params, divid1); $.ajax({ url: page, type: "post", data: params, // callback handler that will be called on success success: function(response, textStatus, jqXHR){ // log a message to the console document.getElementById(divname).innerHTML = response; var retname = 'n_district'; var dropdownIndex = document.getElementById(retname).selectedIndex; var dropdownValue = document.getElementById(retname)[dropdownIndex].value; if(dropdownValue >0) { //alert(dropdownValue); document.getElementById('inputname').value = dropdownValue; } else { document.getElementById('inputname').value = "00"; } return; url2=page2; var params2 = parrams2+'&'; makerequest_sp(url2, params2, divid2); } }); }