danwoods
danwoods

Reputation: 4907

Failing jQuery ajax call

I've just started working with jQuery and love it. I'm having a problem with an ajax call though, and I was wondering if anyone could help me out. Here's 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's my output:

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

my php looks like:

<?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 it's not production quality, but it looks like it should work...

Anyone see why the error function is firing? All help is appreciated. Thanks in advance. Dan

Upvotes: 4

Views: 8089

Answers (4)

Leonardo Nunes
Leonardo Nunes

Reputation: 77

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

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

/* Send the data using post 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"); } });

Upvotes: 1

r00fus
r00fus

Reputation: 2642

Since this is a debugging question, I'll suggest going meta here, and investigating other tools than just a plain browser page:

  1. Use Firebug (getfirebug.com) or Safari/Chrome's webkit inspector:
    http://trac.webkit.org/wiki/Web%20Inspector

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

  3. Look at the actual request and response values

  4. Take Randell's advice an use "print_r()" to debug your php code. I inject the following into my code, and uncomment as needed for debugging things like my SQL call, or the data value before it gets JSON'd, etc:

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

Upvotes: 1

Funka
Funka

Reputation: 4278

Perhaps you could add the dataType parameter (passing "text" as the datatype) to your .ajax call... I usually get a parsererror when getting back malformed XML or JSON. When you don't set the dataType, jquery tries to automatically determine if the response is XML or HTML. Perhaps this is failing, since your response is neither? See http://docs.jquery.com/Ajax/jQuery.ajax#options

Upvotes: 2

David
David

Reputation: 1679

If the request is returning a parser error, then it sounds like the problem is server side. Perhaps improper serialization? Check out your login.php file.

Upvotes: 0

Related Questions