slc
slc

Reputation: 79

Getting JSON data from PHP script via AJAX

My jquery code:

var jsonData;
$.ajax({
    url: 'http://mysite.lv/projects/addform',
    dataType: 'json',
    success: function(response) {
        jsonData = response;
        console.log('Works');
        }
});

My Controller function at http://mysite.lv/projects/addform:

$jsonData = array('x' => 'send x', 'y' => 'send y');
echo json_encode($jsonData);

In console:

GET http://mysite.lv/projects/addform 500 (Internal Server Error)

XHR finished loading: GET "http://mysite.lv/projects/addform".

Upvotes: 0

Views: 56

Answers (1)

Joel
Joel

Reputation: 3060

A 500 error means that your PHP script is failing.

The snippet you added is valid PHP, but the problem is likely elsewhere in the PHP code (or server configuration)

Try debugging by manually loading (that is, open it in your browser) your http://mysite.lv/projects/addform page and making sure it displays the data you expect.

Also enable PHP error reporting — see How to get useful error messages in PHP? here for details.

Upvotes: 2

Related Questions