0

I have passed array as return array($posts,$count) from my ajax file.

now in success function i wnat both this array How to get this array in my success function I have written as below but i dont get any this in my console :( in my ajax_post.php

return array($posts, $count);

   jQuery.ajax({
                            url: 'http://localhost/abc/wp-content/themes/directory/Templates/ajax_post.php',


            data: {
                            'action':'example_ajax_request',
                            'city' : city
                        },
                        success:function(data,count) {
                            // This outputs the result of the ajax request
                            console.log(data,count);

                            //loadPopup(data);
                        },
                        error: function(errorThrown){
                            console.log(errorThrown);
                        }
                    });
User1988
  • 1,965
  • 4
  • 25
  • 47

1 Answers1

0

If your success function is not logging anything at all in your console, then it probably isn't getting called at all.

What do you see in your console? Do you get an error message after your ajax call? What if you try to just copy / past your jQuery.ajax call directly in the console, what happens then?

It's a bit hard to answer here without details of your function on the PHP side and without error messages, but I suggest you try to first return a simple data type from PHP (just a mock value) to make sure your function is indeed getting called and returns properly when called. If that works then you know your problem is with the data type your are returning. If it doesn't, then the problem is with your AJAX call.

You might very well be having problems with AJAX requests because you are doing them on localhost and that might be causing Cross-Domain request problems.

This question / answer actually explains the concept and the solution to that very well. I think you should at least get a reply to your request if you do that : Make cross-domain ajax JSONP request with jQuery

Community
  • 1
  • 1
Hugo Migneron
  • 4,867
  • 1
  • 32
  • 52
  • hello @hugo migneron friend,i didnt get anything in my console not even an error. – User1988 Feb 19 '14 at 05:07
  • hello @hugo migneron,$arr = array(); $arr[0] = $posts; $arr[1] = $count; return json_encode($arr); in my ajax_post.php file.i have written. – User1988 Feb 19 '14 at 05:28
  • OK, and what about the part with cross-domain ajax with JSONP? Did you try the solution suggested there? Do you still not get any reply at all? As I said in my answer, I suggest with a function that does almost nothing on the PHP side. Return the simplest JSON object (for example {"test" : "1"} just to make sure it works and then move up from there. No point in trying with arrays before that works. – Hugo Migneron Feb 19 '14 at 05:31
  • `return json_encode($arr)` ? It should be echoing - `echo json_encode($arr);` and then stop further with - `die('');` – Shazzad Feb 19 '14 at 06:40