0

i have the problem that jquery do not want my json.

here is my jquery code:

        $.ajax({
            type: "POST",
            url: "js/resize.php",
            data: data,
            success: function(data) {       

/* works
          var data = [
                    {"og_src":"img\/base\/logo.png","src":"img\/base\/das-logo.png"},
                    {"og_src":"\/img\/studio\/lounge-2.JPG","src":"\/img\/studio\/lounge-2.JPG"},
                    {"og_src":"\/img\/studio\/desk.JPG","src":"\/img\/studio\/desk.JPG"}
                    ];

*/

    // console.log(data);
                $.each(data, function(key, image){
                 console.log(image);
                    var el = $("img[rel='"+image.og_src+"']");
                    ...

If I copy the sent data to my script and make a var data = ... it works fine.

The php data are made by a simple echo json_encode($stack);

If I make a php-echo like

echo '[{"og_src":"img\/base\/logo.png","src":"img\/base\/das-logo.png"}, {"og_src":"\/img\/studio\/lounge-2.JPG","src":"\/img\/studio\/lounge-2.JPG"}, {"og_src":"\/img\/studio\/desk.JPG","src":"\/img\/studio\/desk.JPG"}]';

it do not work ether.

All files are on UTF-8.

Console.log shows the result of console.log(image); as a single letter; Whats wrong, I'am trying this for hours now and I'am very desperated. Thanks for an help in advance.

3
  • 1
    You could try to specify dataType: 'json'. Commented Apr 15, 2013 at 10:25
  • Thx Jack, I made it several times without to specify dataType: 'json'. And it allways worked correct. I added it for now. And it works fine. I do not know why it worked in the past, but here it is my solution. Commented Apr 15, 2013 at 10:31
  • Then you're missing the correct Content-Type response header as mentioned by Quentin. Commented Apr 15, 2013 at 10:32

2 Answers 2

2

It looks like your JSON is being interpreted as HTML (so when you run each of it, it loops over each character of the HTML source).

Make sure that your PHP script says that it is outputting JSON (PHP defaults to HTML).

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

You could also pass the dataType: "json" option to ajax so that the jQuery library will try to parse the result of the HTTP request as JSON, no matter what the server says it is. Having correct information in the HTTP response is a much cleaner solution though.

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

4 Comments

thx Quentin dataType: 'json' works for me. I tried to find why it worked in the past. With your suggestion I have the same result. echo json_encode($stack); header("Content-Type: application/json"); But must be wrong because console.log is interpreting the result still as html. So we are on the right way.
You have to output the HTTP headers before the HTTP body. You should have got error messages when you tried it that way around.
Sorry I'am not the php crack; If I do it the otherway around I get the error message header Cannot modify header information - headers already sent by
I have it: Was a bom Problem. Thanks
1

You should tell jQuery that the returned data is expected to be 'json' using the dataType property:

$.ajax({
        type: "POST",
        url: "js/resize.php",
        data: data,
        dataType: 'json',
        success: function(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.