0

while trying to echo back some html on jsfiddle using jquery deferreds, I'm not getting any data back.

function showData(data1, data2) {
    console.log(data1[0]);
    console.log(data2);
}

function method1() {
    return $.ajax({
        type: "post",
        url: "/echo/html/", 
        data: JSON.stringify("test1"),
        dataType: 'html'
    });
}

function method2() {
   return $.ajax({
        type: "post",
        url: "/echo/html/", 
        data: {data: "test2"},
        dataType: 'html'
    });
}

$.when(method1(), method2()).then(showData);

I can't understand what I've done wrong here. Passing the data as an object or as JSON.stringify, neither seems to work. http://jsfiddle.net/VAy5g/

4

2 回答 2

0

尝试使用像 deferred.pipe() 这样的异步方法。stackoverflow 线程 10253192 jquery - 了解 Deferred.pipe() 中显示了一些示例。

这是与您的问题相关的 jquery 文档,http://api.jquery.com/jQuery.when/。仔细阅读并查看 jQuery.ajax() 文档以获取有关 ajax 请求成功和错误情况的完整描述。准确地调整when的原型,然后将函数分开。确保console.log 在这个地方做某事并改变它,因为你的函数做你希望它做的事情以及它应该做的事情。登录文本文件可能会更好。

于 2013-05-29T19:33:35.460 回答
0

对这个问题的回答有点迟。

问题不在于你的 jQuery。这很好用。唯一的问题是您使用 jsFiddle 的/echo/htmlAPI。比较您的代码:

data: {data: "test2"},

工作代码

data: {html: "test2"},

指定响应的属性是data.html,不是data.data

此版本的代码按预期工作。

于 2013-06-07T16:44:55.003 回答