我有一个Ajax响应的问题。我有一个将Json发送到服务器的ajax请求,并且响应也会返回一个json。jquery responseText undefined
我收到响应,但无法到达responseText。这下面有我的代码:
$('#outside').click(function() {
var formData = '{"nin":"3","etype":"iso","perror":"1000","filename":"C:/Users/Mary/workspace/tdiclient/helloworld.txt"}'// _ sono i due punti e -e il bckslash
var response = '';
var posts= $.ajax({
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
'url' : 'http://localhost:8080/com.tdi/rest/tdi/newcontent/parameters',
'type' : 'POST',
'data' : formData,
responseText: {
success: true,
mode: "Online"
}
})
console.log(posts);
if(posts.readyState == '1'){
alert(posts.responseText);
}
});
的问题是,我得到以下错误:
Cannot read property 'responseText' of undefined
我一派,我尝试了很多解决方案,但找不到解决方案
看起来你似乎在混合原生XHR对象和JQuery包装器。为什么在代码中定义'responseText'而不是设置一个可以访问它的“succes”回调函数? –
可能的重复[如何从异步调用返回响应?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) –
@mary:鉴于您提供的代码示例,应该没有错误。 '$ .ajax()'方法会将一个对象返回到'posts',所以'posts.responseText'不会产生这个错误。当然,它可能没有你想要的数据。 – 2017-02-21 20:45:53