【发布时间】:2017-01-01 07:50:13
【问题描述】:
我想要一个网页,当按下按钮时,浏览器会使用 POST 方法导航到一个 URL,以及一些 xml 数据作为请求内容。响应也将是 xml。我的问题类似于 JQuery AJAX Post - pass variable and navigate URL ,但该问题的答案是 ajax POST,然后导航到 URL 的 GET,而我希望通过 POST 直接导航。
到目前为止我尝试了什么?
<html>
<head>
<title>Entry page</title>
<script src="js/lib/jquery/jquery-3.1.0.min.js"></script>
<script>
function LogIn() {
$.ajax({
url: "http://localhost:8080/Login/" + $( "#User" ).val(),
data: '<m:password xmlns:m="http://acme.com">' + $( "#Password" ).val() + "</m:password>",
method: 'POST',
type: "POST",
contentType: "application/xml",
dataType: "xml",
success :function()
{
alert('Ok');
window.location="http://localhost:8080/Login/" + $( "#User" ).val();
},
error : function (xhr, ajaxOptions, thrownError){
console.log(xhr.status);
console.log(thrownError);
} });
}
</script>
</head>
<body>
<form>
<label>User<input id="User" type="text" value="Sean" name="User"/></label>
<label>Password<input id="Password" type="password" name="Password"/></label>
</form>
<button onclick="LogIn()">Try it</button>
</body>
</html>
跨域
我意识到这是跨域。服务器已配置为允许这样做,并且跨域位运行良好。
有什么问题?
$.ajax() 调用只是调用 POST,它不会导航到它。设置 window.location 没有帮助,因为这是一个无内容的 GET,而不是一个加载数据的 POST。
请指教。
【问题讨论】: