How to use JSON with Jquery?

前端 未结 3 1707
盖世英雄少女心
盖世英雄少女心 2020-12-06 07:39

I\'m working at an app which would make a POST ajax request to a PHP script on my server. The script would query the database and return a row of records, as an array. (One

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 08:05

    1) in the php script:

    $return["foo"]  = "bar";
    $return["blah"] = "bleg";
    print json_encode($return);
    

    2) in the javascript:

    $.ajax({
      type: "POST",
      url: URL,
      cache: false,
      data: values,
      dataType: 'json',
      success: function(json) {
        var foo = json.foo;
        if (json.blah == "bleg") {
          // do stuff 
        }
      } // end success function
    }); // end ajax call
    

提交回复
热议问题