如何在ajax中使用json模式

时间:2014-12-18 05:16:52

标签: ajax json

我尝试使用此源代码将json连接到ajax,但我不知道为什么这个源代码不起作用?

这是源代码:

    var my_JSON_object = {};

var http_request = new XMLHttpRequest();
http_request.open("GET", url, true);

http_request.onreadystatechange = function ()
{
  var done = 2, ok = 100;
  if (http_request.readyState == done &&
      http_request.status == ok) {

    my_JSON_object = JSON.parse(http_request.responseText);
  }
};
http_request.send(null);

i hope someone can give me a solution.

var my_JSON_object = {};

var http_request = new XMLHttpRequest();
http_request.open("GET", url, true);

http_request.onreadystatechange = function ()
{
  var done = 2, ok = 100;
  if (http_request.readyState == done &&
      http_request.status == ok) {

    my_JSON_object = JSON.parse(http_request.responseText);
  }
};
http_request.send(null);

1 个答案:

答案 0 :(得分:0)

简单的方法是使用jquery - 然后你的ajax代码可以像

一样简单
$.post("ajax_directory/yourfile.php",
    {para: para, parb: parb, parc: parc, pard: pard, pare: pare},
    function(data) { what you want to do with the data )."json");

如果你把那个决赛" json"您甚至可以以json格式返回数据,并根据其ID将其发送到页面上的不同项目。

para等是您设置的变量,整个事情可以包含在对按钮点击的响应中,选择更改,键盘等