开发者

Another jQuery Ajax Request Error

开发者 https://www.devze.com 2023-03-23 08:00 出处:网络
Sorry for asking such a question again. I\'ve been looking for a solution for hours but nobody could help me. So maybe someone here knows what the problem is.

Sorry for asking such a question again. I've been looking for a solution for hours but nobody could help me. So maybe someone here knows what the problem is. I was writing a login form, which sends entered data to an PHP script on the server. The PHP script should do the work. All connections are secured so sending clear passwords is no problem.

My login form (login.php) looks something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="/layout/main_style.css" rel="stylesheet" type="text/css" />
    <script src="/models/jquery.js" type="text/javascript"></script>

    <script type="text/javascript">

    function submitLogin() {

    $.post("login_process.php", { username: "TEST" },
       function(data) {
         alert("Data Loaded: " + data);
       });

     }

    </script>
    <title>Login</title>



    </head>
    <body>
    <div id="loginwrapper">
        <div id="content">

            <div id="main">

            <h1>Login</h1>


            <div id="errors">

            </div>     


                <div id="regbox">
                    <form name="newUser" onsubmit="submitLogin()" method="post">
                    <p>
                        <label>Username:</label>
                        <input type="text" name="username" />
   开发者_JAVA技巧                 </p>

                    <p>
                         <label>Password:</label>
                         <input type="password" name="password" />
                    </p>

                    <p>
                        <label>&nbsp;</label>
                        <input type="submit" value="Login" class="submit" />
                    </p>

                    </form>

                </div>
            </div>

                <div class="clear"></div>
            </div>
    </div>

    </body>
    </html>

and my process file (login_process.php) looks like this:

<?php
header('Content-type: application/json');
echo json_encode("OK");
exit;
?>

I'm distressed. Why doesn't it show a message saying "OK" ? :(


If this line gives you an error:

data: "user="&document.forms["newUser"]["username"].value&"&password="&document.forms["newUser"]["password"].value,

Its fairly simple, & is a BOOLEAN AND operator in JavaScript. Use the + operator instead for string concatenation.

Edit 1

The code appears to work perfectly OK on my system with one minor change:

onsubmit="submitLogin(); return false;"


Two suggestions:

  1. Add &callback=? at the end of data for .ajax() and modify login_process.php so:

    header('Content-type: application/json');

    $return['msg'] = "OK";

    echo $_POST['callback']."('".json_encode($return)."');";

    exit;

When running the success functions "name" is inserted in place of ? in the callback=?. And what you are essentially doing in your php script is "calling" this success function.

  1. Use XMLHttpRequest so:

    var xhr = new XMLHttpRequest();

    var data = new FormData();

    data.append("msg", "I am now testing you");

    data.append("data", "blahblahblah");

    xhr.open('POST', 'login_process.php', true);

    xhr.onreadystatechange = function() {

    if(xhr.readyState == 4 && xhr.status == 200)
    
        alert("YAY!");
    

    }

    xhr.send(data);

0

精彩评论

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

关注公众号