0
<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>
我需要提交表单并查看它是否成功。该文件正在我的服务器端上传,没有错误,但阿贾克斯说它失败(失败的警报显示)。我打开控制台,它给人的错误:Jquery 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.
所以窗体被上传,因为我可以看到它是从我的数据库。我GOOGLE了错误,似乎我可以在我的AJAX中使用jsonp作为数据类型。我试过这个,但它不起作用,但我宁愿不将它用于安全风险,我也不需要这种数据类型。
为什么downvote? – user2924127
我没有看到下降投票的理由这个问题..嘿伙计,我看到你的代码中没有错误,我已经检查过它。提交后它完美地发布表单内容。我认为你的服务存在一个问题,写在http://111.111.111.111:5008/form .. ajax等待特定时间段的响应,如果它不会那么它会抛出错误事件....或检查此http ://stackoverflow.com/questions/15412226/access-control-allow-origin-error你可能需要设置代理服务器,以重定向到实际服务器的Ajax调用(你必须编写重写规则,以重定向).Upvoted ;) –