如何从response中仅获取responseText,以便使用ajax和jquery使用该dat
我只需要对象的responseText,这样我就可以使用数据填充字段如何从response中仅获取responseText,以便使用ajax和jquery使用该dat,jquery,Jquery,我只需要对象的responseText,这样我就可以使用数据填充字段 var request = $.ajax({ url: "http://jsonplaceholder.typicode.com/posts/1", type: "GET", dataType: "application/json" }); console.log(request); 在request中有responseText,但无法检索它。我尝试使用request.r
var request = $.ajax({
url: "http://jsonplaceholder.typicode.com/posts/1",
type: "GET",
dataType: "application/json"
});
console.log(request);
在request中有responseText,但无法检索它。我尝试使用request.responseText,然后它显示为未定义,因此可以帮助我如何获取它。我对ajax和jquery是新手尝试以下方法:
$.ajax({
url: "http://jsonplaceholder.typicode.com/posts/1",
type: 'GET',
dataType: "application/json",
error: function() {
},
success: function(response) {
console.log(response)
}
});
您需要添加一个成功/错误函数 这是你的电话:
$.ajax({
url: "http://jsonplaceholder.typicode.com/posts/1",
type: "GET",
dataType: "application/json",
// function goes here
});
该函数将如下所示:
success:function(details){
// details is return data; assign to an object
}
试试这个。这将满足您的要求
$.ajax({
url: "http://jsonplaceholder.typicode.com/posts/1",
type: "GET",
dataType: "jsonp",
success: function (response) {
console.log(response);
}
});
您可以使用
成功
,错误
,和done
事件来操作响应对象。响应在控制台中打印,不打印调用该函数所需的任何内容。@roy是的,有一个错误,请尝试更新的代码above@JayeshChitroda直到相同,它不会在控制台中打印任何内容。我是jquery新手。我如何添加详细信息jsfiddle how to do it'details'只是一个参数,它已经包含在成功回调中了。因此,您无需声明它,您可以根据您的偏好,随意命名它;)@是的,我已经编辑了答案。谢谢你的详细解释@roy顺便说一句,当您有一个类型:json时,您不能通知/console.log(),如果您想知道返回的字符串,请使用console.log(json.stringify(details))并关闭类型:json.In console.log我可以检索数据如何使用该响应将其存储在变量中并全局使用,以便我可以使用所有数据在此成功函数使用response.userId、response.id、response.title、response.body&通过变量存储。例如:var UserId=response.UserId,var Id=response.Id…像这样存储您想要的值!罗伊,如果我的回答对你有帮助的话。请投票!嗨,我不想将数据类型用作jsonp我如何才能将数据类型保持为json使用数据类型:“json”