1

我正在向远程服务器发出 Ajax 请求并将参数作为 POST 方法发送。但我得到以下回应:

**"MLHttpRequest cannot load http://rasovai.com/mobilecontact1.php?_dc=1369189135731. Origin null is not allowed by Access-Control-Allow-Origin.  "**

我读到了这个错误,发现是因为 CORS,所以我在请求中添加了 header,如下所示:

 Ext.Ajax.defaultHeaders = {
                                        'Accept': 'application/json',
                                        'Accept': 'Access-Control-Allow-Origin: *',
                                        'Accept': 'Access-Control-Allow-Credentials:   true',
                                          'Accept': 'Access-Control-Allow-Methods: OPTIONS, GET, POST',
                                        'Accept': 'Access-Control-Allow-Headers: Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control'
                                    };

但我仍然收到同样的错误响应。

我能够点击服务器上的 url,但无法传递参数。

任何人都可以在这方面帮助我吗?

谢谢伊山耆那教

4

3 回答 3

2

这些标头需要由服务器而不是客户端发送。

于 2013-05-22T02:53:58.383 回答
0

如果您使用的是 Chrome 浏览器,则可以使用--disable-web-security标志来允许跨域请求,如果您将其构建到应用程序中,这将正常工作。查看此线程以获取更多详细信息:How to use json proxy to access remote services during development

于 2013-05-22T06:51:27.347 回答
0

如果要将参数传递给服务器,则可以按以下方式进行。

 Ext.Ajax.request({
            url : serverURL,
            jsonData : requestParams,  //  Object which encapsulates the request params
            method : 'POST',
            withCredentials: true,
            useDefaultXhrHeader: false,

           // List of header params can be sent as follows
            headers : {
                "Content-Type" : "application/json",
                "Accept" : "application/json",
                "Access-Control-Allow-Origin":"http://localhost:8080",
                "Authorization":auth
            },

            username : 'mobiliser',
            password : 'secret',
            success : function(response) {
             }
            failure : function ()
             {
             }

正如您在问题中所说,服务器位于不同的域中,您可能必须使用 JSONP 请求。

如果您有任何问题,请告诉我。

谢谢- Gendaful

于 2013-05-22T19:37:55.580 回答