0

The jquery ajax get call in the function below is supposed to pass some parameters to test.php, have test.php perform some tests based on these parameters, and return the (jason encoded) test result (status_general) to .get(). However, the console.log call in the code below shows a value undefined, while my php debugger shows that test.php returns the proper jason encoded test result. My .get code is based on example 5 of http://api.jquery.com/jQuery.get.

Any suggestions what goes wrong/how to fix it? Thanks in advance for the help.

Code:

$.get( "test.php", { command: "test", val1: ipaddress, val2: "" })
.done(function(data) {
    var test_result = data.status_general; 
     console.log('test result: '+test_result);              
},"json");
The Alpha
  • 131,979
  • 25
  • 271
  • 286
Joppo
  • 685
  • 1
  • 10
  • 29
  • 4
    Try console logging data to see what the server is returning – Zeke Nierenberg Oct 14 '13 at 18:40
  • replace `done` with `success`. – gdoron is supporting Monica Oct 14 '13 at 18:40
  • @zeke: console.log(data) gives a lot of text starting with "
    – Joppo Oct 14 '13 at 18:49
  • @gdoron: replacing done with success: no console info is shown at all... – Joppo Oct 14 '13 at 18:49
  • 1
    Your response to Zeke makes it seem like only a fragment of `data` is valid JSON. The entire response needs to be a valid JSON structure. Can you paste the value of `data` (you can collapse long string values down to `""` for brevity). Try `JSON.stringify(data, null, 2);` for a good pretty-print. – ach Oct 14 '13 at 18:57
  • The `xdebug-e` bit makes me assume there's a PHP error occurring in `test.php` that's output as an XDebug debugging table... – AKX Oct 14 '13 at 19:05

2 Answers2

0

I'd suggest that you should make sure that your server is setting the correct Content-Type header for JSON (see What is the correct JSON content type?).

According to the jQuery documentation:

Note: We must ensure that the MIME type reported by the web server matches our choice of dataType.

So, I'd suggest using FireBug or Chrome's network traffic debuggers, to confirm that the right Content-Type is being sent (and to see the actual data being returned from the server). If you use the right Content-Type, Chrome, at least, shows you pretty-printed JSON in the network tab.

Community
  • 1
  • 1
pioto
  • 2,082
  • 19
  • 34
  • thnx for the suggestion; but i wonder whether the content type would be wrong, the reason being that I already used used another (type) of ajax get() call (without passing parameters) also set to receive json encoded data and that call works fine. I just changed/corrected a few things that helped to create the proper json format, e.g. '{"status_general":"success"}' but monitoring data.status_general still shows a value undefined??? – Joppo Oct 14 '13 at 19:54
0

Your PHP page isn't returning valid json (based on the comment you said). In order to get jquery to parse the json, it can't have any other stuff in it (ie html tags). Also a good idea to send the data-type json headers.

http://www.caveofprogramming.com/php/php-json-an-example-javascript-json-client-with-php-server/

The data.status_general is returning undefined because data isn't a javascript object.

Zeke Nierenberg
  • 2,079
  • 1
  • 16
  • 29
  • thnx, adding 'header('Content-type: application/json')' in your link solved the problem. Though I still dont understand why all the other .get() calls I used without the header worked fine (?) – Joppo Oct 14 '13 at 20:43