简体   繁体   English

跨源ajax“ POST”请求失败

[英]Cross Origin ajax “POST” request failing

I have a webservice(REST) running on localhost:8080, to call the webservice I use this jquery code: 我有一个运行在localhost:8080上的webservice(REST),要调用此jquery代码使用的webservice:

jQuery.ajax({
        type: "POST",
        url: "http://localhost:8080/user/register",
        data: '{"name": "' + name + '","email": "' + email + '","password": "' + password + '"}',
         beforeSend: function(x) {
              if(x && x.overrideMimeType) {
               x.overrideMimeType(jsonMimeType);
              }
             },
        dataType:"jsonp",
        Accept : "application/json",
        contentType: "application/json",
        success: registerUser_success_callback,
        error: registerUser_error_callback
    });

When I try to call this javascript from the same domain(ie localhost:8080), it work just like a charm!. 当我尝试从同一个域(即localhost:8080)调用此javascript时,它就像一个超级字符一样起作用! Here is the screen shot of the same: 这是相同的屏幕截图: 在此处输入图片说明

But when I try to access the same from a different domain(ie localhost:80), it fails, and surprisingly instead of a POST, it sends out a GET and I also get a log in my server's log file, saying that the GET method not supported for REST resource. 但是,当我尝试从其他域(即localhost:80)访问相同文件时,它失败了,令人惊讶的是,它发出了GET消息,而不是POST消息,而且我还在服务器的日志文件中得到了一条日志,说GET REST资源不支持该方法。 Here is the screen of the same: 这是相同的屏幕: 在此处输入图片说明

From what I have read on internet(especially here , great article!), cross domain request should first send out an OPTIONS request(which is cached for later usage.) What is going wrong in my case? 从我在Internet上阅读的内容(尤其是在这里 ,很棒的文章!),跨域请求应该首先发出一个OPTIONS请求(将其缓存以备后用。)我的情况出了什么问题?

I have tried calling the rest service with same parameters using FireFox's plugin RESTClient, I was able call the rest service(POST method) successfully, so this mostly is the issue with ajax part. 我尝试使用FireFox的插件RESTClient调用具有相同参数的rest服务,我能够成功调用rest service(POST方法),所以这主要是ajax部分的问题。

Help me resolve this cors hell! 帮我解决这个问题! and do lemme know if I need to share any more details on this. lemme是否知道我是否需要分享更多详细信息。

PS: After @A. PS:@A之后。 Wolff Suggested, I changed the data tyoe from jsonp to json, now my browser sends out OPTIONS, but after that it doesn't send the actual POST request!!! Wolff建议,我将数据类型从jsonp更改为json,现在我的浏览器发出了OPTIONS,但此后它不再发送实际的POST请求!

Well, some more digging and I found the solution to this! 好吧,再挖一些,我找到了解决方案!

After the change A. Wolff suggested, browser was sending out an OPTIONS request and was receiving a 200 response, but after that actual POST request was not being sent to the server. 在更改A. Wolff建议之后,浏览器正在发送OPTIONS请求并收到200响应,但是在该实际POST请求未发送到服务器之后。

Problem was, if you send any specific headers, "Content-Type" in my case, server has to add that header in "Access-Control-Allow-Headers" field of the response(response of OPTIONS request). 问题是,如果在我的情况下,如果发送任何特定的标头“ Content-Type”,则服务器必须在响应(OPTIONS请求的响应)的“ Access-Control-Allow-Headers”字段中添加该标头。 I changed my server side code to populate that field in the response header, and VOILA! 我更改了服务器端代码,以在响应标头中填充该字段,然后VOILA!

Hope this will be helpful to others, too! 希望这对其他人也有帮助!

Happy Coding. 编码愉快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM