0

Im really getting so frustrated with this jquery, I want o use the getJson for my ajax but its just not working! its driving me nuts.

i have this, got this from an example:

 $.getJSON("../file.php",{filepath:g_filepath, startRow: "0"},dates);

and also this:

 $.ajax({
                type: "GET",
                url: '../file.php',
                data: param,
                async: true,
                beforeSend: function(x) {
                if(x && x.overrideMimeType) {
                x.overrideMimeType("application/j-son;charset=UTF-8");
                }
            },
            dataType: "json",
            success: function(data){
                //do your stuff with the JSON data
                 alert("called");
            }
            });

Both are just not working, its not getting the reponse.

Can somebody help me.

In my php file codes goes something like this:

$myArray =  array();
 while($filehandle->eof) {

  $line = $filehandle->fgets();

  array_push($myArray, $line);
  echo json_encode($myArray);  


}

am I doing it wrongly?

tinks
  • 313
  • 1
  • 4
  • 21
  • 1
    are you sure the server returns something? – Joseph Mar 07 '12 at 06:26
  • 2
    Do not invent MIME-types. `application/json` is the right one. – kapa Mar 07 '12 at 06:28
  • The server might return an error? Use Chrome Developer Tools or similar to look at the request/response – jgauffin Mar 07 '12 at 06:29
  • there are a good number of things could be wrong..if don't know how to use a browser console to look at request, learn now, the status of request is the first place to start. Also could be a script error and request isn't even happening, again need to look in console – charlietfl Mar 07 '12 at 06:32
  • Are you sure the returned JSON is valid? -- [Ajax request return 200 OK but error event is fired instead of success](http://stackoverflow.com/questions/6186770/ajax-request-return-200-ok-but-error-event-is-fired-instead-of-success) – Salman A Mar 07 '12 at 06:33
  • It goes to the callback function already but response is returning null. In my php, i have an array and I echo the json_encode(); Im using the firefox/firebug.. and its not returning response data – tinks Mar 07 '12 at 06:40
  • edited my question and added my php code.. – tinks Mar 07 '12 at 06:48
  • Like I said, you must first check if your JSON is valid. The PHP script would produce something like `["line1"]["line2"]["line3"]` which is invalid JSON. – Salman A Mar 07 '12 at 07:05
  • but if you use json_encode($array) it should be okay... @Salman A – tinks Mar 07 '12 at 07:15
  • `json_encode($array)` will work when it is used the _right_ way. Post the JSON returned by server. – Salman A Mar 07 '12 at 07:56

3 Answers3

0

I'd try to avoid giving url a relative pathname. Change that to the full path to your target page. Similarly, ensure that target page actually returns content.

Are you using Chrome or Firefox/Firebug? Enable the Network output and follow the request. Does it make a proper request?

Morgon
  • 3,109
  • 1
  • 25
  • 32
  • It goes to the callback function already but response is returning null. In my php, i have an array and I echo the json_encode(); Im using the firefox/firebug.. and its not returning response data – tinks Mar 07 '12 at 06:39
  • does php file print when open it in browser? – charlietfl Mar 07 '12 at 06:56
  • tried using it like this http://stackoverflow.com/questions/9513377/onreadystatechange-in-ajax-not-working and just made it to responseText.. i can get a response.. but when i use jquery its just not returning – tinks Mar 07 '12 at 07:07
  • But you mentioned that your target PHP file didn't output when called directly. That sounds like an issue with your target file rather than jQuery. – Morgon Mar 07 '12 at 13:21
0

With these sort of problems work incrementally.

First test the service directly from a browser.

Once you've got a URL that works in the browser then put it into your JQuery.

Indeed I usually start by mocking up the service by creating a JSON text file and serving it from a web server, see that in the browser, then get my JavaScript going and finally write the service.

djna
  • 52,574
  • 11
  • 70
  • 109
0

php should return just one json object not many.. you returning json in each while loop call

WRONG:

$myArray =  array();
 while($filehandle->eof) {

  $line = $filehandle->fgets();

  array_push($myArray, $line);
  echo json_encode($myArray);  


}

SHOULD WORK:

$myArray =  array();
 while($filehandle->eof) {

  $line = $filehandle->fgets();

  array_push($myArray, $line);
}
echo json_encode($myArray);  

also consider firebug or other javascript debug tool for testing further errors

Vytautas
  • 3,469
  • 1
  • 24
  • 42