Error when calling jQuery ajax

I just started working with jQuery and love it. However, I have a problem with ajax call, and I was wondering if anyone could help me. Here is my ajax call:

//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); } }); 

and here is my conclusion:

xrs.status = 200
throw error = undefined
xhr.statusText = OK
request = undefined
error = undefined

my php looks like this:

 <?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; } ?> 

I know that this is not product quality, but it looks like it should work ...

Does anyone see why the error function is triggered? All help is appreciated. Thank you in advance. Dan

+4
source share
4 answers

Perhaps you could add the dataType parameter (pass "text" as the data type) to your .ajax call ... I usually get parsererror when returning invalid XML or JSON. When you do not set dataType , jquery tries to automatically determine if the response is XML or HTML. Perhaps this is not so, because your answer is neither one nor the other? See http://docs.jquery.com/Ajax/jQuery.ajax#options

+2
source

Since this is a debugging issue, I suggest going here and exploring other tools besides a simple browser page:

  • Use Firebug (getfirebug.com) or the Safari / Chrome web browser inspector:
    http://trac.webkit.org/wiki/Web%20Inspector

  • Learn how to use these tools, especially the Net (firebug) or Resources (webkit) panels.

  • Look at the actual request and response values

  • Take Randell for using "print_r ()" to debug your php code. I paste the following into my code and uncomment when it is necessary to debug things like my SQL call, or data values ​​before it gets JSON'd, etc.:

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

+1
source

/ * stop the form from submitting normally * / event.preventDefault();

/ * get some values ​​from the elements on the page: * / var val = $(this).serialize();

/ * Send the data using a message and put the results in a div * / $.ajax({ url: "newsletter.php", type: "post", data: val, datatype: 'json', success: function(data){ alert('SUCCESS'); }, error:function(){ alert("ERROR"); } });

+1
source

If the request returns a parser error, then it sounds like a server-side problem. Perhaps the wrong serialization? Check your login.php file.

0
source

All Articles