使用if变量的javascript函数

时间:2013-11-25 20:16:55

标签: javascript

如果success: function(result)错误而不是alert(),但如果确实如此,我想重定向。任何人都可以帮我解决这个问题。

 function log_me_in_now() {

    var eml = $('#email').val();
    var pwd = $('#password').val();

    $.ajax({
        url: 'http://dev/ajax/shopper_login.php',
        data: { 'email': eml, 'password': pwd },
        dataType: 'json',
        cache: false,
        failure: function(results) {
            alert('99 problems...\n' + results);
        },
        success: function(result) {
            alert(result);
            //window.location = "http://dev/test/logged_in"
        }
    });
}

AJAX:

include_once('../includes/settings.php');

if (isset($_GET['email']) && $_GET['email'] != '' && isset($_GET['password']) && $_GET['password'] != '') {

    // todo: change their connection string to become their Oracle user.
    $shopper = new Shopper($_GET['email']);
    $token = $shopper->login($_GET['password']);

    if (isset($token) && $token != '') {
        echo json_encode($token);
    }
}

3 个答案:

答案 0 :(得分:1)

            success: function(result) {
                if (result == "true") {
                    window.location = "http://dev/test/logged_in"                    
                }
                else{
                    alert("Login Failed -- Please try again.");                    
                };
            }

这取决于你的ajax调用实际返回的内容。从理论上讲,如果登录正确,它应该返回“true”或“false”。如果这是真正的呼叫返回的内容,那么上面的代码将适合您。

答案 1 :(得分:0)

检查一下:

function log_me_in_now(){

var eml = $('#email').val();
var pwd = $('#password').val();

$.ajax({
    url: 'http://dev/ajax/shopper_login.php',
    data: { 'email': eml, 'password': pwd },
    dataType: 'json',
    cache: false,
    failure: function(results) {
        alert('99 problems...\n' + results);
    },
    success: function(result) {
        window.location.assign("http://dev/test/logged_in");
    }
});

}

答案 2 :(得分:0)

检查一下! 对于默认浏览器,您的提交必须为preventdefault false!

$("#inputID" ).click(function( event ) {
  event.preventDefault();
var eml = $('#email').val();
var pwd = $('#password').val();

  $.ajax({
      url: 'http://dev/ajax/shopper_login.php',
      data: { 'email': eml, 'password': pwd },
      dataType: 'json',
      cache: false,
      failure: function(results) {
        alert('99 problems...\n' + results);
      },
      success: function(result) {
        window.location.assign("http://dev/test/logged_in");
      }
    });
});