crbast
crbast

Reputation: 2292

JQuery specific problem - PHP JSON return

I have a problem with processing a JSON object.

When I do echo the result I get this:

result : undefined

Image of undefined result

$.post("./php/date_information.php", function(temp){
        alert(temp.weekNumber); // HERE
        $('select[name="week"]').val(temp.weekNumber);
      });

but when I change my code I get this

result : {"year":"2018","weekNumber":"39","status":"200"}

Image of correct

$.post("./php/date_information.php", function(temp){
        alert(temp); // Here
        $('select[name="week"]').val(temp.weekNumber);
      });

PHP script return (JSON FORMAT):

{"year":"2018","weekNumber":"39","status":"200"}

I have no idea why JQuery doesn't take temp.weekNumber. Any ideas?

Upvotes: 1

Views: 67

Answers (1)

Set the content type to json in your PHP script, before echoing the content

header('Content-Type: application/json');

Upvotes: 1

Related Questions