125*_*748 1 javascript ajax jquery jsfiddle jquery-deferred
我正在尝试对jsfiddle进行jquery ajax调用但是遇到了问题:
var ajax1 = function () {
return $.ajax({
type: "post",
url: "/echo/json/",
data: {
name: "thomas!"
},
dataType: 'json'
});
};
var res = ajax1();
console.log(res);
Run Code Online (Sandbox Code Playgroud)
将整个延迟对象打印到控制台.它包括responseText我认为可能是我应该尝试访问的内容,但我未定义.
console.log(res.responseText);
Run Code Online (Sandbox Code Playgroud)
ajax返回一个promise对象,而不是ajax请求的结果.您需要注册成功回调以获取ajax请求返回的值
它应该是
var ajax1 = function () {
return $.ajax({
type: "post",
url: "/echo/json/",
//also the format for json request is as follows
data: {
json: JSON.stringify({
name: "thomas!"
})
},
dataType: 'json'
});
};
var res = ajax1();
res.done(function (data) {
console.log(data)
})
Run Code Online (Sandbox Code Playgroud)
演示:小提琴
| 归档时间: |
|
| 查看次数: |
651 次 |
| 最近记录: |