使用Jquery发送和接收数据

时间:2012-09-23 09:10:03

标签: javascript ajax json

我处于基本的应用程序开发阶段。

我想知道如何使用JQuery发送和获取这些数据的任何新版本。

我也希望它支持所有浏览器。

我只是使用简单的Ajax,但我知道Jquery可以使用它,但我无法弄明白。

function SendData() {
    var data = "action=check&uid=" + uid + "&fbuid=" + fb_uid + ";
    var url = "http://www.example.com/call.php";
    var ajax = new AJAXInteraction(url, CheckRate);
    ajax.doPost(data);
};

function CheckRate(Content) {
    response = JSON.parse(Content);
    Rate = response.stat.rate;
    document['getElementById']('ERate')['value'] = Rate;
};

function AJAXInteraction(url, callback) {
    var req = init();
    req.onreadystatechange = processRequest;
    function init() {
        if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    function processRequest() {
        if (req.readyState == 4) {
            if (req.status == 200) {
                if (callback) callback(req.responseText);
            }
        }
    }
    this.doGet = function () {
        req.open("GET", url, true);
        req.send(null);
    }
    this.doPost = function (str) {
        req.open("POST", url, true);
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        req.send(str);
    }
};

我能够解决第一部分仍然不知道休息:

function SendData(){
    dataString = "action=check&uid=" + uid + "&fbuid=" + fb_uid + ";
    url = "http://www.example.com/call.php";
    jQuery.ajax({
        type: "POST",
        url: url,
        data: dataString,
    });
};

我的问题是我将如何阅读回复

function CheckRate(Content) {
    response = JSON.parse(Content);
    Rate = response.stat.rate;
    document['getElementById']('ERate')['value'] = Rate;
};

1 个答案:

答案 0 :(得分:0)

function SendData() {
    dataString = "action=check&uid=" + uid + "&fbuid=" + fb_uid + ";
    url = "http://www.example.com/call.php";
    jQuery.ajax({
        type: "POST",
        url: url,
        data: dataString, // sending data
        success: function (data) {
            CheckRate(data); // receiving data
        }
    });
};

// function body should looks like this
function CheckRate(Content) {
    response = JSON.parse(Content);
    Rate = response.stat.rate;
    document['getElementById']('ERate')['value'] = Rate;
};