0

I'm using Jquery and ajax.

I have a simple form and my jquery :

This is a piece of the code :

username = $('input[name="username"]').val(); 
$.post("api.php", {username: username}, function(data) {  
    if(data == "error") {
        data("erreur");
    } else {
        alert(data);
        $('input[name="subscribers"]').attr("placeholder", "something").blur();
        $('input[name="viewCount"]').attr("placeholder", "something").blur();
    }
});

And the result of alert(data);

{"total":"628729","abo":"1646"}

I would like to put the result of total and the result of abo into my placeolder :

$('input[name="subscribers"]').attr("placeholder", ?abo?).blur();

But i don't know who to recover the result of the json and take the value of total and abo

note : my json is genrate by the file api.php with json_encode

7
  • 1
    Try to add a 4th parameter to the $.post call: $.post('api.php', {username: username}, function(data){}, 'json'), to tell jQuery to parse the JSON. Then data` will be an object (not a function). Then you can access data.total and data.abo. Commented Jan 16, 2014 at 16:19
  • have a look at: api.jquery.com/jquery.parsejson Commented Jan 16, 2014 at 16:20
  • Also, in case of an error, don't reply with "error". Reply with {error:true}, so that you can have jQuery parse it as JSON and check for data.error Commented Jan 16, 2014 at 16:23
  • @RocketHazmat if i use your solution with the parameter , 'json' it's works fine, but i can also use "JSON.parse(data); ", which one is better ? Commented Jan 16, 2014 at 16:25
  • @Martialp: I prefer not to have to manually call JSON.parse, but it's up to you. Commented Jan 16, 2014 at 16:27

4 Answers 4

1

jQuery appears to be trying to handle your JSON as text (or, more likely, HTML).

Tell jQuery that it is JSON:

<?php header("Content-Type: application/json"); ?>

Then you can just:

foo = data.total
Sign up to request clarification or add additional context in comments.

2 Comments

If there is an error, it should be handled with an HTTP status code and an error handler as well as a success handler. (But it doesn't stop that, you can do Content-Type: text/plain if you want to send back a plain text message body and then the comparison will work).
it's something that he is echo in response,else case he response the json data
0

Try this:

if (data) {
    data = JSON.parse(data);
    $('input[name="subscribers"]').attr("placeholder", data.abo).blur();
}

1 Comment

@RocketHazmat You are right. If you can change your server side code, then you should handle it at server side and use data as it is. Agreed!
0

You can use $.parseJSON() to parse json

username = $('input[name="username"]').val(); 
$.post("api.php", {username: username}, function(data) {  
    if(data == "error") {
        data("erreur");
    } else {
        alert(data);
        data=$.parseJSON(data); // add this line
        $('input[name="subscribers"]').attr("placeholder", "something").blur();
        $('input[name="viewCount"]').attr("placeholder", "something").blur();
    }
});

Comments

0

JSON is a string representing (in this case) an object, so data is a string. In order to go from the string to the object you need to parse it. There's a JSON.parse() function to do that:

if(data == "error") {
    data("erreur");
} else {
    alert(data);
    var yourObj = JSON.parse(data);
    $('input[name="subscribers"]').attr("placeholder", yourObj.abo).blur();
    $('input[name="viewCount"]').attr("placeholder", yourObj.subscribers).blur();
}

3 Comments

Why use JSON.parse, when you can tell jQuery to do it for you by setting Content-Type: application/json or passing 'json' as a 4th parameter to $.post?
@RocketHazmat Mostly because I'm lazy and the implicit parsing from jQuery would break the if(data == "error") test. You're correct, though, a better solution would be to have jQuery implicitly parse it and return a property in the object that indicates whether there was an error.
I guess, though I'd suggest on an error responding with {error:true} so you could check for data.error.

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.