简体   繁体   English

jQuery Ajax不起作用-否'Access-Control-Allow-Origin'

[英]Jquery ajax not working - No 'Access-Control-Allow-Origin'

<html>
    <head>
        <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$( document ).ready(function() {

$('#my-form')
  .submit( function( e ) {
    $.ajax( {
      url: 'http://111.111.111.111:5008/form',
      type: 'POST',
      data: new FormData( this ),
      processData: false,
      contentType: false,
      success: function (data) {
       alert("SUCCESS");
      },
      error: function (textStatus, errorThrown) {
       alert("FAILED");
    }
  } );
    e.preventDefault();
  } )


});
</script>   
    </head>
    <body>
        <div>
            <form id="my-form">
                    <div>
                        File:
                            <input type="file" name="file" />
                    </div>
                    <div>
                        <button type="submit">Submit</button>
                    </div>
            </form>
        </div>
    </body>
</html>

I need to submit a form and see if it was successful. 我需要提交表格,看看是否成功。 The file is being uploaded on my server side and there are no errors, but ajax is saying it is failing (Failed alert showing). 该文件正在我的服务器端上载,没有错误,但是ajax说它失败了(显示失败警报)。 I opened the console and it gives the error: 我打开控制台,它给出了错误:

XMLHttpRequest cannot load http://111.111.111.111:5008/form. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://111.111.111.111' is therefore not allowed access.

So the form is being uploaded because I can see it is from my database. 因此正在上载表单,因为我可以从数据库中看到它。 I googled the error and it seems I can use jsonp as a datatype in my ajax. 我在错误中进行了搜索,看来我可以在Ajax中将jsonp用作数据类型。 I tried this and it did not work, but I would rather not use this for the security risks and I have no need for that data type. 我尝试过此方法,但它没有用,但我不希望将其用于安全风险,并且不需要该数据类型。

I have found the issue. 我发现了问题。 From my server the response back needs to modify the header. 从我的服务器返回的响应需要修改标头。 The header needs to allow all origins. 标头需要允许所有来源。 Here is an example with Java (vertx.io). 这是Java(vertx.io)的示例。

ctx.response().putHeader("Access-Control-Allow-Origin ", "*");

Once I added this to my server code, the requests and responses work as needed. 将其添加到服务器代码后,请求和响应将按需工作。

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

相关问题 jQuery Ajax登录中没有“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' Header In jQuery Ajax Login jquery.ajax Access-Control-Allow-Origin - jquery.ajax Access-Control-Allow-Origin 访问控制允许起源不适用于POST请求中的Ajax - Access-control-allow-origin not working with ajax in a POST request 服务器响应状态为401-无“ Access-Control-Allow-Origin”标头-Angular Js-与Jquery Ajax配合正常 - Server responded with a status of 401 - No 'Access-Control-Allow-Origin' header - Angular Js - working fine with Jquery Ajax Ajax-“访问控制允许来源”错误 - Ajax - “Access-Control-Allow-Origin” Error Access-Control-Allow-Origin无法正常工作 - Access-Control-Allow-Origin not working correctly PHP-Access-Control-Allow-Origin无法正常工作 - PHP - Access-Control-Allow-Origin not working “访问控制允许来源”:“*”不起作用 - 'Access-Control-Allow-Origin ': '*' is not working Jquery (jfeed) - Access-Control-Allow-Origin 不允许来源 xxxxx - Jquery (jfeed) - Origin xxxxx is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin不允许使用Origin null(同步,无jQuery) - Origin null is not allowed by Access-Control-Allow-Origin (Synchronous, no jQuery)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM