[英]Submit clicks does not response when use AJAX
我正在编写HTML代码,在其中尝试使用AJAX,但是每当我点击“提交”按钮时,对我来说似乎都没有用。
但是在我的代码中不使用AJAX,我的表单就可以正常工作,所以可能是什么原因,请参见下面的HTML代码(使用AJAX)不起作用:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(function () {
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'get',
url: 'http://www.domain.com/offers/api.php',
data: $('form').serialize(),
success: function () {
alert('form was submitted');
}
});
});
});
</script>
<title>Untitled Document</title>
</head>
<body>
<form>
<input type="text" name="fname" />
<input type="text" name="lname" />
<input type="hidden" name="uname" value="amit" />
<input type="hidden" name="ukey" value="suri" />
<input name="submit" type="submit" value="Submit">
</form>
</body>
</html>
这是我的旧HTML脚本,可以很好地工作:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form method="get" action="http://www.domain.com/offers/api.php" >
<input type="text" name="fname" />
<input type="text" name="lname" />
<input type="hidden" name="uname" value="amit" />
<input type="hidden" name="ukey" value="suri" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
看起来这是问题所在:
Cross-Origin Request Blocked: The Same Origin Policy disallows
reading the remote resource at [url].
This can be fixed by moving the resource to the same domain or enabling CORS.
相同起源政策说明: http : //www.jquery-tutorial.net/ajax/same-origin-policy/
可能的解决方法: http : //blog.edwards-research.com/2012/10/cross-domain-ajax-a-simple-workaround/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.