有什么问题,我认为是一个相对简单的jQuery插件…

插件应该通过ajax从php脚本中获取数据,并将选项添加到<select>。ajax请求非常通用:

$.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'类型:

::1 - - [08/Jul/2009:11:43:27 -0500] "OPTIONS sitecodes.php HTTP/1.1" 200 46

为什么这个ajax调用可以在Safari中工作,但不能在Firefox中工作,我如何为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

下面是Firebug输出的图片:


当前回答

我使用代理url来解决类似的问题,当我想要发布数据到我的apache solr托管在另一个服务器。(这可能不是完美的答案,但它解决了我的问题。)

使用模式重写代理,我添加这一行到我的httpd.conf:

 RewriteRule ^solr/(.*)$ http://ip:8983/solr$1 [P]

因此,我可以将数据发布到/solr,而不是发布到http://ip:8983/solr/*。然后它将在相同的源中发布数据。

其他回答

我已经发布了一个明确的例子,如何解决这个问题,如果控制服务器代码的域名,你要张贴。这个答案在这篇文章中有所涉及,但在我看来,这篇文章更清楚地解释了这个问题。

如何通过JavaScript发送跨域POST请求?

另一种绕过这个问题的方法是使用代理脚本。例如,这里描述了该方法

我在尝试使用Facebook API时也遇到了类似的问题。

唯一没有发送preflightrequest的contentType似乎只是文本/plain…而不是mozilla中提到的其他参数

为什么这是唯一一个这样做的浏览器? 为什么Facebook不知道并接受飞行前的请求?

供你参考:前面提到的Moz文件建议X-Lori报头应该触发preflightrequest…它不是。

 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);

     }
});         
}

响应脚本顶部的PHP似乎可以工作。(使用Firefox 3.6.11。我还没有做很多测试。)

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
if(array_key_exists('HTTP_ACCESS_CONTROL_REQUEST_HEADERS', $_SERVER)) {
    header('Access-Control-Allow-Headers: '
           . $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
} else {
    header('Access-Control-Allow-Headers: *');
}

if("OPTIONS" == $_SERVER['REQUEST_METHOD']) {
    exit(0);
}