1

I have found many threads regarding this issue, but unfortunately I couldn't get it running. Problem is I don't know much about JQuery.

I am trying to make an Ajax call using JQuery in order to fetch multiple records from a mysql database. I have the following function :

function updateWebpage () 
{
$.ajax({                                      
url: './sale/api.php',                  //the script to call to get data          
data: "",                  //you can insert url argumnets here to pass to  api.php
                                   //for example "id=5&parent=6"
  dataType: 'json',                //data format      
  success: function(rows)          //on recieve of reply
  {
        for (var i in rows)
        {
          var row = rows[i];          

          var username = row[0];
          var stateId = row[1];
          $('#output').append("<b>id: </b>"+username+"<b> stateId: </b>"+stateId)
                      .append("<hr />");
        } 
    } 
});

};

My api.php is executing a mysql query with something like this:

$array = retrieveUsersInfo('%');                          //fetch result    
echo json_encode($array);

My main issue, is how to debug an issue like this? Since ajax is calling asynchronously another file, I cannot view any errors. From my firefox debugger, I can see that the $.ajax function is entered, but success is not.

Thanks in advance.

2
  • 2
    Have you tried calling api.php directly from a browser? Does it return valid JSON? Does it return an array as you appear to be expecting? Commented Feb 12, 2013 at 20:20
  • 1
    Try to implement error function as well Commented Feb 12, 2013 at 20:21

1 Answer 1

1

a couple things to try.

  1. hit the api url directly in a browser (not through ajax) and make sure it returns the valid response.
  2. add an error: function(err){} to your jquery ajax call. this method will get called if there is something other than a 200 response back from the server.
  3. I use Chrome's developer tools more than firefox/firebug. It has a Network tab in it that shows me all the communication between the client and the server. You should see a call out to your api in that tab.

just off hand, i think you need to make sure the mime-type is set to text/json in your php file.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. adding an err function helped me seee what was wrong!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.