![](/img/trans.png)
[英]No 'Access-Control-Allow-Origin' Header In jQuery Ajax Login
[英]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>
我需要提交表格,看看是否成功。 該文件正在我的服務器端上載,沒有錯誤,但是ajax說它失敗了(顯示失敗警報)。 我打開控制台,它給出了錯誤:
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.
因此正在上載表單,因為我可以從數據庫中看到它。 我在錯誤中進行了搜索,看來我可以在Ajax中將jsonp用作數據類型。 我嘗試過此方法,但它沒有用,但我不希望將其用於安全風險,並且不需要該數據類型。
我發現了問題。 從我的服務器返回的響應需要修改標頭。 標頭需要允許所有來源。 這是Java(vertx.io)的示例。
ctx.response().putHeader("Access-Control-Allow-Origin ", "*");
將其添加到服務器代碼后,請求和響應將按需工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.