失败的jQuery ajax调用

时间:2009-09-01 02:57:57

标签: jquery ajax

我刚开始使用jQuery并且喜欢它。我遇到了ajax电话的问题,我想知道是否有人可以帮助我。这是我的ajax电话:

//start the ajax
    $.ajax({
        //this is the php file that processes the data and send mail
        url: "php/login.php",   

        //GET method is used
        type: "GET",

        //pass the data         
        data: data,     

        //Do not cache the page
        //cache: false,

        //success
        success: function (html) {              
            //if process.php returned 1/true (send mail success)
            if (html==1) {                  
                //hide the form
                alert( html );
                $('.form').fadeOut('slow');                 

                //show the success message
                $('.done').fadeIn('slow');

              //if process.php returned 0/false (send mail failed)
              } else alert('Sorry, unexpected error. Please try again later.' + html);              
    },

    error:function (xhr, ajaxOptions, thrownError, request, error){
      alert('xrs.status = ' + xhr.status + '\n' + 
            'thrown error = ' + thrownError + '\n' +
            'xhr.statusText = '  + xhr.statusText + '\n' +
            'request = ' + request + '\n' +
            'error = ' + error);
      }       


    });

这是我的输出:

xrs.status = 200
抛出错误=未定义
xhr.statusText = OK
request = undefined
error = undefined

我的php看起来像:

<?php

//turn on error reporting, set header
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
header('Content-Type: text/xml');

//pull variables
//Need to do some error checking here
$username = $_GET['name'];
$password = $_GET['pass'];

//connect with database
$con = mysql_connect("localhost","root","");
//if connection unsuccessful
if(!$con){
  //stop, and display error
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("musicneverstopped", $con);
//end connecting to database

//query database for user-submitted username and store result in $result
$result = mysql_query("SELECT * FROM users WHERE username = '$username'");

//if no results returned
if(!$result){
  //stop and display error
  die(mysql_error());
  }

//check if a single result was returned
if(mysql_num_rows($result) == 1){
  //if true, set the returned results to $row
  $row = mysql_fetch_array($result);
  //check if password from user matches password from database
  if($password == $row['password']){
    //if true, begin session
    session_start();
    //assign session variables
    $_SESSION['username'] = $row['username'];
    $_SESSION['privilege'] = $row['privlege'];
    //send user to index page
    //header('Location: http://localhost/musicneverstopped');
    mysql_close($con);//close mysql connection
    return 1;
    }
  else{
    mysql_close($con);//close mysql connection
    //if false, send user to login page
    return 0;
    }
  mysql_close($con);
  }//end if(mysql_num_rows($result) == 1)
else{
  mysql_close($con);//close mysql connection
  return 0;
  }
?>

我知道这不是生产质量,但它看起来应该有效......

任何人都能看到错误功能被解雇的原因?所有帮助表示赞赏。提前致谢。丹

4 个答案:

答案 0 :(得分:2)

也许您可以添加dataType参数(将“text”作为数据类型)添加到.ajax调用中......在获取格式错误的XML或JSON时,我通常会得到parsererror。如果未设置dataType,jquery会尝试自动确定响应是XML还是HTML。也许这是失败的,因为你的回答都不是?见http://docs.jquery.com/Ajax/jQuery.ajax#options

答案 1 :(得分:1)

由于这是一个调试问题,我建议在这里使用meta,并调查其他工具而不仅仅是普通的浏览器页面:

  1. 使用Firebug(getfirebug.com)或Safari / Chrome的webkit检查员:
    http://trac.webkit.org/wiki/Web%20Inspector

  2. 了解如何使用所述工具,尤其是Net(firebug)或Resources(webkit)面板。

  3. 查看实际的请求和响应值

  4. 根据Randell的建议,使用“print_r()”来调试你的php代码。我将以下内容注入到我的代码中,并根据需要取消注释以调试SQL调用之类的内容,或者在获取JSON之前的数据值等等:

  5.   

    die("<pre>".print_r($phpvariable,true)."</pre>");

答案 2 :(得分:1)

/ *停止表格正常提交* / event.preventDefault();

/ *从页面上的元素中获取一些值:* / var val = $(this).serialize();

/ *使用post发送数据并将结果放入div * / $.ajax({ url: "newsletter.php", type: "post", data: val, datatype: 'json', success: function(data){ alert('SUCCESS'); }, error:function(){ alert("ERROR"); } });

答案 3 :(得分:0)

如果请求返回解析器错误,那么听起来问题是服务器端。也许不正确的序列化?查看您的login.php文件。