JQuery的阿贾克斯头问题 [英] JQuery-Ajax headers issue

查看:175
本文介绍了JQuery的阿贾克斯头问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到当我试图做一个POST请求使用JSON
头有问题 这是code:

I got a problem with the headers when i'm trying to do a POST Request with JSON
This is the code:

$.ajax({
                type: "POST",
                url: url,
                data: jsonData,
                dataType: 'json',
                beforeSend: function(xhrObj){
                    xhrObj.setRequestHeader("Content-Type","application/json");
                    xhrObj.setRequestHeader("Accept","application/json");
                },
                error: function(){
                    alert("Fail");
                },
                success: function(){
                    alert("Success");
                }
            });

这是由萤火虫所显示的请求头。

And this are the Request Headers displayed by Firebug.

OPTIONS /path HTTP/1.1
Host: 192.168.15.109:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0 FirePHP/0.7.1
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
DNT: 1
Connection: keep-alive
Origin: http://localhost
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
x-insight: activate
Pragma: no-cache
Cache-Control: no-cache

和响应报头:

HTTP/1.1 204 No Content
Date: Thu, 24 May 2012 19:17:01 GMT
Allow: OPTIONS,POST

正如你所看到的,标题犯规匹配那些IM指定,但是当我使用卷曲头是这样的人,而不是:

As you can see, the headers doesnt match with the ones im specifying, but when i use CURL the Headers are this ones instead:

POST /path HTTP/1.1
User-Agent: curl/7.25.0 (i386-pc-win32) libcurl/7.25.0 OpenSSL/0.9.8u zlib/1.2

Host: localhost:8080
Accept: */*
Content-Type: application/json
Content-Length: 5

任何想法或解决方案呢?

Any idea or solution for this?

我也修改的JQuery源设置阿贾克斯发送到JSON的头的默认值,但没有工作。

I also modified JQuery Source to set default values of the Headers sent by Ajax to JSON, but didnt work.

推荐答案

其实这是一个跨领域的问题,我定义我的网址为IP,所以浏览器跨preTED它像一个跨域请求。

Actually it was a cross Domain problem, i defined my URL as an IP, so the browser interpreted it like a Cross Domain request.

感谢大家! (:

这篇关于JQuery的阿贾克斯头问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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