trancur1_12xx trancur1_12xx - 5 months ago 12
JSON Question

Javascript - if Ajax request (json /htmls response includes/= something)

I have a question i guess its pretty simple but ive tried many things and couldnt get it to work.

My function:

function dosomething() {
$.ajax({
type : "POST",
url : 'test.php',
data : 'a=1&b=2&d=Y',
success : function (data) {
ajax_success(data);
}
});}


the response looks like:

{
"message": "<h3> something.... </h3>",
"y": {
"a": 1234,
"b": 53342,
"c": 77782 }};


now how how can i "scan" the result ? as example i want to call a other function Z() if the the response of the request includes/ is: y.a > 4000

the point is i have no idea how do i check that response of the first request.
ive learned a lot in the last years also via code academy it was very helpful, but it would be very nice if someone could give me an answer to it :)
Regards

Kld Kld
Answer

You can just check if(data.y.a > 400)

function dosomething() {
  $.ajax({
    type : "POST",
    url : 'test.php',
    data : 'a=1&b=2&d=Y',
    success : function (data) {
        if(data.y.a > 400){
             z();
        } 
    }
});
}

function Z(){
 //do something
}
Comments