可以´t从对象XMLHttpRequest获取服务器响应
Can´t get server response from object XMLHttpRequest
由于REST调用,我无法从对象XMLHttpRequest获得响应。我知道结果在对象内部,因为当我有结果要返回时,对象的大小会更大,但我无论如何都无法访问它们。这是我对服务器的javascript请求:
function getMarkersByCategory(category) {
var urlServer = 'http://localhost:8080/api/mapit/getcategory';
return loadContent(urlServer,category);
}
function loadContent(url, category) {
var mypostrequest = new ajaxRequest();
mypostrequest.onreadystatechange = function() {
if (mypostrequest.readyState == 4) {
if (mypostrequest.status == 200 || window.location.href.indexOf("http") == -1) {
return displayMarkers(mypostrequest);
}
else {
alert("An error has occured making the request");
}
}
}
var parameters = "?category=" + category ;
mypostrequest.open("GET", url + parameters , true);
mypostrequest.send(null);
}
function ajaxRequest() {
var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
if (window.ActiveXObject) { //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
for (var i = 0; i < activexmodes.length; i++) {
try {
return new ActiveXObject(activexmodes[i]);
}
catch (e) {
//suppress error
}
}
}
else if (window.XMLHttpRequest) // if Mozilla, Safari etc
return new XMLHttpRequest();
else
return false;
}
function displayMarkers(data) {
alert(data);
var jsonContent = data // I can´t find any property of the object with the response
}
最后,这是我的Java web服务的响应:
@GET
@Produces("text/plain")//I have tried with ("application/json") too
@Path("/getcategory")
public String getByCategory(@QueryParam("category") String category) {
List<MapItBean> list = mapItPointDao.getMapItPointsByCategory(category);
String result = MapItBeanHelper.jsonizeMapitList(list);
System.out.println(result);
return result;
}
我也试过使用jquery,但我遇到了同样的问题,我无法得到任何响应。提前谢谢。
看起来您当前正在将XMLHttpRequest
或ActiveXObject
本身作为data
的值传递给displayMarkers
。
// ...
return displayMarkers(mypostrequest);
来自服务器的响应将存储在其responseText
属性中,如果是JSON,则需要对其进行解析。
// ...
return displayMarkers(JSON.parse(mypostrequest.responseText));
当然,假设发出此请求的页面也在http://localhost:8080/
上。如果涉及另一个来源(或者在file://
的情况下没有来源),则需要做更多的工作来允许请求。
相关文章:
- XMLHttpRequest未返回值-状态202
- XMLHttpRequest在移动设备上的chrome上不起作用
- AJAX简单错误.XMLHttpRequest无法加载http://localhost/mpl/getPage.php.
- 检查xmlhttprequest问题的消息
- 主线程上的同步XMLHttpRequest已弃用
- 如何定期发出xmlhttprequest
- javascript第三个XMLHttpRequest被拒绝.但前两个是允许的
- 上传带有XMLHttprequest的文件-多部分/表单数据中缺少边界
- 对象#<XMLHttpRequest>没有方法'完成'
- 在XMLHttpRequest之后重新初始化jQuery
- 如何将GreaseMonkey中的XMLHttpRequest延迟到目标页面加载完成
- Javascript XMLHttpRequest——只有第一个POST请求有效
- XMLHttpRequest - difference between Chrome & Firefox
- IE上的新XMLHttpRequest()出现JS Ajax未指定错误
- 非常简单的XMLHttpRequest不起作用
- 如何使用XMLHttpRequest下载文件
- XMLHttpRequest在$.ajax工作的地方给出了CORS错误
- XMLHttpRequest.open()AJAX中的参数url
- XMLHTTPRequest脚本中没有internet连接和超时
- 为什么当async标志设置为false时,xmlhttprequest中的代码可以工作,而当它设置为true时却不能工作