jQuery $.ajax(), $.post 发送“选项"作为 Firefox 中的 REQUEST_METHOD [英] jQuery $.ajax(), $.post sending "OPTIONS" as REQUEST_METHOD in Firefox

查看:13
本文介绍了jQuery $.ajax(), $.post 发送“选项"作为 Firefox 中的 REQUEST_METHOD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为是一个相对简单的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆