Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Below ajax code is receiving a json array from php. Rest details i have written as comments:

 $.ajax({
     type: "POST",
     url: "dbtryout2_2.php",
     data: datastr,
     cache: false,
     //dataType: 'json',
     success: function (arrayphp) {
         //"arrayphp" is receiving the json array from php.
         //below code is working where iam displaying the array directly
         //This code displays the array in raw format.       
         $(".searchby .searchlist").append(arrayphp);
     }

 });

FRIENDS CONCENTRATE ON THIS SECTION.NOW I WILL MAKE U THE PROBLEM MORE CLEAR AND EXACT: 1)success function is having two code 2)one is uncommented and other is commnented 3)the commneted code works if i comment code "dataType: "json"", 4)but the uncommneted code does not work with the situation which the below code currently has

 $.ajax({
     type: "POST",
     url: "dbtryout2_2.php",
     data: datastr,
     dataType: "json",
     cache: false,
     success: function (arrayphp) {
         $.each(arrayphp, function (i, v) {
             alert(i + "--" + v);
         });
         /*$(".searchby .searchlist").append(arrayphp);*/
     },
     error: function (xhr) {
         console.log(xhr.responseText);
     }

 });

BELOW IS THE PHP CODE SNIPPET RESPONSIBLE FOR RETURNING JSON ARRAY:

 $arrayphp = array();
 //$result is containing the list of albums
 //iam one by one taking the album names and assigning it to $row
 //then from $element iam pushing the elements in $arrayphp
 //after pushing all the elements iam returning the json encoded array to ajax code.  
 while ($row = mysql_fetch_array($result)) {
     $element = $row['cat_name'];
     array_push($arrayphp, $element);
 }
 echo json_encode($arrayphp);
 }

THE ARRAY RETURNED IS A LIST OF ALBUM NAMES:

["album1", "album2", "album5", "album4", "album6", "album7", "album8", "album9", "album10", "album11"]

THE EXACT ABOVE ARRAY IS GETTING RETURNED.

WOULD ANYBODY FIGURE OUT WAT THE PROBLEM IS WITH MY CODE?

share|improve this question
 
check firebug or some other browser debugger - sounds like the problem is the way your trying to loop the json .. can you add the returned data to your question ? –  ManseUK Aug 31 '13 at 19:17
1  
could you give a little detail on the structure of the return? –  james emanon Aug 31 '13 at 19:17
 
pleas, post the json it retrieves –  dscdsc Aug 31 '13 at 19:18
1  
Use jQuery.parseJSON() api.jquery.com/jQuery.parseJSON –  Vicky Thakor Aug 31 '13 at 19:21
 
I don't think you need to parseJSON from an $.ajax call. You should try console.log(arrayphp); in your success method, and see what comes out in the console under developer tools. You should also add this after success to see if you are getting an error: error: function(xhr){ console.log(xhr.responseText); } –  Jake Aug 31 '13 at 19:34
show 5 more comments

2 Answers

try this in your ajax success:

var i = 0;
$.each(arrayphp, function () {
      alert(arrayphp[i]);
      i++
 });
share|improve this answer
add comment

I tried your function and it worked fine for me when I left out this line: contentType: "application/json", and when I added it in, I started getting a failed response of "0".

In the manual, it says to use this content type for most cases: 'application/x-www-form-urlencoded; charset=UTF-8' (which is the default). So unless you have a good reason, I think you should remove that line.

In the first function, it doesn't exist and you turned off 'json' dataType, so you are probably alerting a really nice looking string that looks like the data you want. If you make sure that you have dataType: 'json' and remove contentType: application/json', you should be good to go.

EDIT: This is exactly what I used, and it worked great. Not sure what else to suggest. What is datastr in your js?

$.ajax({
    url:      'dbtryout2_2.php',
    type:     'post',
    async:    true,
    cache:    false,
    dataType: 'json',

    success: function( response )
    {
        // response is returned as json object: ["album1","album2","album3","album4","album5"]
        //console.log(response);

        if ( response )
        {
            $.each(response, function (i, v) {
                alert(i + "--" + v);
            });
        }
    },

    error: function( xhr )
    {
        console.log(xhr.responseText);
    }
});

Note: If you have specified "dataType:'json'", you don't have to $.parseJSON, because the response is already returned as JSON.

share|improve this answer
 
@ jake still its not working.see the code above . i have clarified my problem again. –  Nitish kumar thakur Sep 1 '13 at 8:30
 
well finally the above code is working.U know actually the problem was in the data that i was returning from php. I was echoing two things from php one was a "message" that i had written initially for checking whether php code is running or not and the other was the "array" that actually i wanted to return.But i was not at all concentrating on the string that was getting returned with the array. –  Nitish kumar thakur Sep 3 '13 at 16:21
1  
well thanks a lot to all of you for your suggestions –  Nitish kumar thakur Sep 3 '13 at 16:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.