AJAX请求检索XML输出和大小写重定向..但事实并非如此;t重定向


AJAX request to retrieve XML output and on case redirect... but it doesn't redirect

一旦重定向检索到XML,它就会出现问题。它是一个获取ID用户名和ID密码值并检查其是否存在的登录。在这种情况下,它显示警报"工作",但不会重定向页面,我不知道为什么。

点击后的查询功能如下:

$(document).ready(function(){
    $("#submit").click(function(){
        login();
    function login() {
        $.ajax({
          type: 'POST',
          url: 'dt_login.php',
          async: false,
          cache: false,
          data: {
            params: {
              op: 'authenticate',
              username: $("#username").val(),
              password: $("#password").val()
            }
          },
          dataType: 'xml',
          success: function(xml) {
            switch($(xml).find('message').text()) {
              case 'OK':
              alert( "worked" );
                window.location = 'http://www.newlocation.com';
              break;
              case 'DENIED':
                alert( "Incorrect username or password." );
              break;
              default:
                alert( "defualt" );
              break;
            }
          }
        });
    }
    });
});

dt_login.hp:

<?php
function api($params) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://exampleurl.com');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    $output = curl_exec($ch);
    curl_close($ch);
    print $output;
    $xml = new SimpleXMLElement($output);
    $_SESSION['user_id']= $xml->user_id;
}
if(is_array($_POST['params'])) { api($_POST['params']); }
?>

输出:150OK1111swatkinsSAM WATKINS

有什么想法吗?为什么它显示警报但不会重定向?

您没有取消表单提交,因此设置了竞赛条件。表单已提交,您正在尝试重定向。您需要取消表单提交。

$("#submit").click(function (evt){ 
    evt.preventDeafult(); 

理想情况下,您还可以连接到表单上的onsubmit处理程序,而不是单击按钮。使用submit作为按钮名称是个坏主意,因为它会与submit() 发生名称冲突