开发者

I need a help with this script ($.ajax())

开发者 https://www.devze.com 2023-02-10 08:07 出处:网络
Ajax: $.ajax({ url: \'process.php\', type: \'post\', data: \'loginName=\' + $(\"#loginName\").val() + \'loginPass=\' + $(\"#loginPass\").val(),

Ajax:

$.ajax({
   url: 'process.php',
   type: 'post',
   data: 'loginName=' + $("#loginName").val() + 'loginPass=' + $("#loginPass").val(),
   dataType: 'json', 
   success: function(data){
    if(data.success)
    {
        location.href = data.redirect;
    }
    else
    {   
        alert(data.message);
    }
    }
});

And here is process.php code:

<?
$data = array();
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    if($_POST[开发者_开发问答'loginName'] == "test" && $_POST['loginPass'] == "test")
    {
        $data['redirect'] = "home.php";
        $data['success'] = true;
        echo json_encode($data);
    }
    else
    {
        $data['message'] = "<div id=Message>Your info is wrong...</div>";
        $data['success'] = false;
        echo json_encode($data);
    }
}
?>

Sorry, but I'm from the Czech Republic and I don't speak english :D


Your data: misses a & right before loginPass=, which would lead to a garbled request. But maybe you should give jQuery an data object there anyway (it takes care of proper url encoding):

type: 'POST',
data: {loginName: $("#loginName").val(), loginPass: $("#loginPass").val()},

A second problem might be the lack of content-type in the php script. Add following on top (just to be sure this isn't why the result goes ignored):

<?php
   header("Content-Type: application/json");

Another thing: You need more braces in the if statement, because && has higher precedence than the == comparison:

if(($_POST['loginName'] == "test") && ($_POST['loginPass'] == "test")) {

Since it is seemingly a PHP error, you must turn on error_reporting(E_ALL) and display_errors in the php.ini; If you still get no content returned from your URL, then add a print 'test123'; on top. And/or remove the test for REQUEST_METHOD (this adds no security anyway).

0

精彩评论

暂无评论...
验证码 换一张
取 消