weixin_33725270 2016-08-25 02:30 采纳率: 0%
浏览 40

jQuery AJAX导航为POST

I want a web page, which when a button is pressed, the browser navigates to a URL with method POST, and some xml data as the request content. The response will also be xml. My question is similar to JQuery AJAX Post - pass variable and navigate URL , but the answers to that question do an ajax POST, followed by navigation to a GET of the URL, whereas I want a direct navigation via POST.

What I have tried so far?

<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>

Cross Domain

I realise that this is cross domain. The server has been configured to allow this, and the cross-domain bit works perfectly well.

What is the issue?

The $.ajax() call simply invokes the POST, it doesn't navigate to it. Setting the window.location doesn't help, because this is a content-less GET, not a data-loaded POST.

Please advise.

  • 写回答

2条回答 默认 最新

  • 胖鸭 2016-08-25 03:00
    关注

    The post request is posting the data to:

    http://localhost:8080/Login/" + $( "#User" ).val()

    This is on you local host so you should have a server side route on you application to catch this ajax post request and send a success/message back back. Try passing in a "data" parameter into your success function and see what it logs.

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作