1

I'm trying to get data returned from a function in a PHP file via AJAX (using Chrome)

The JS:

    $.ajax({
            url: 'http://www.site.co.uk/api/file.php',
            data: data, 
            dataType: 'json', 
            type: 'POST', 
            contentType: 'application/json',
            async: false,
            success: function (jsonData){
                console.log("SUCCESS");
                var responseText = jQuery.parseJSON(jsonData.responseText);
                console.log(responseText);
            },
            error: function (jsonData){
                console.log("ERROR");
                var responseText = jQuery.parseJSON(jsonData.responseText);
                console.log(responseText);
            }
        });

Relevant part of the PHP:

public function post_method()
{
    $data['error_message'] = "Error message text";
    return json_encode($data);
}

In the console I'm getting:

POST http://www.site.co.uk/api/file.php 500 (Internal Server Error)
ERROR
Object {status: "{", message: "{"}

In the PHP, if I replace

return json_encode($data);

with

return $data;

the console shows

SUCCESS
Uncaught SyntaxError: Unexpected token u jquery.js:3 

Not sure why I can't get 'error_message', and not sure why using 'json_encode' appears to produce a 500 error.

@Johannes Reuter - It's slightly complicated but I'm calling post_method() like this:

public function route_method()
{
   switch($this->resource['request_method'])
    {
        case 'POST':
            return $this->post_method();
            break;
        default:
            return FALSE;
            break;                
    }

}

This is part of some api framework code that I didn't write. '$this->resource' is essentially being passed into the class constructor. The same method is used successfully in other PHP files.

@Rory McCrossan - how should I define it? I thought it was OK just to create it as an array.

6
  • Can you show where you define $data in your PHP code and what it contains. The second error is because you've told jQuery to deserialise a JSON response, yet the response was not JSON. Commented Feb 27, 2015 at 11:02
  • Could you add the code that is calling post_method()? Commented Feb 27, 2015 at 11:04
  • Use die() instead of return - die(json_encode($data)); or echo and die combo. Commented Feb 27, 2015 at 11:06
  • Check if you are echo your data in your php file. You should echo it and not return it. Commented Feb 27, 2015 at 11:09
  • What data are you passing using data is it form.serialize() or else Commented Feb 27, 2015 at 11:25

3 Answers 3

1

If you inform dataType as json in the request, just dont need use jQuery.parseJSON function because it's json.

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

Comments

0

you will try.

echo json_encode($data);exit;

Comments

0

use echo json_encode($data);die; instead of return json_encode($data);

Comments

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.