使用 jsfiddle 和 jQuery 模拟 ajax 响应

Simulate ajax response with jsfiddle and jQuery

我可以在 jsfiddle 中模拟 ajax 响应吗? http://doc.jsfiddle.net/use/echo.html 描述了如何使用 new Request.HTML,但是,它似乎仅适用于 Mootools,并且在与 jQuery (ReferenceError: Request is not defined) 一起使用时出现错误。例如,在我下面的示例中,我想在单击 "start" 时提醒 "returned data"。

<button id="start">start</button>

/*
new Request.HTML({
    url: '/echo/html/',
    data: {
        html: "returned data",
        delay: 3
    },
    method: 'get',
    update: 'target'
}).send();
*/

$('#start').click(function () {
    $.get("/echo/html/", function (data) {
        alert(data);
    });
})

http://jsfiddle.net/naaarcnk/

jsfiddle 给出的例子使用了 mootools,然而,你可以用任何语言做同样的请求,你只需要模仿它的方法和数据。

new Request.HTML({
    url: '/echo/html/', // the target url
    data: {  // the data to send (next 3 lines too)
        html: "returned data",
        delay: 3
    },
    method: 'get', // the method to use
    update: 'target' // target html element to update
}).send();

jQuery方式:

$('target').load('/echo/html/', {html: 'returned data', delay: 3});

$.post('/echo/html', {html: 'returned data', delay: 3}, function (result) {
    console.log(result);
});