English 中文(简体)
JQuery- Ajax 信头问题
原标题:JQuery-Ajax headers issue

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

这是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

正如你所看到的,信头与所指定者不匹配, 但是当我使用 CURL 时,信头就是这些:

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

有什么想法或解决办法吗?

我还修改了 JQuery 来源, 以设定 Ajax 发送给 JSON 信头的默认值, 但无效 。

最佳回答

事实上,这是一个跨域的问题, 我将我的 URL 定义为 IP, 所以浏览器将它解读成 跨域请求 。

谢谢你所做的一切!

问题回答

似乎是一个 same - sexist 问题。 使用 < code> dataType=jsonp 应该有效, 但这可能需要其他修改 。

https:// developmenter.mozilla.org/en/http_access_control ,以深入解释。





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...