2013-11-26 5 views
0

REST 호출의 결과로 XMLHttpRequest 객체로부터 응답을받을 수 없습니다. 결과가 돌아올 때 객체 크기가 더 커지기 때문에 결과가 객체 내부에 있음을 알지만 어쨌든 나는 그 객체에 액세스 할 수 없습니다. 내가 할 수있는,XMLHttpRequest 객체에서 서버 응답을 얻을 수 없습니다.

@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를 사용하여뿐만 아니라 시도하지만 같은 문제가 있습니다 :

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 
} 

그리고 마지막으로이 내 자바 웹 서비스의 응답이이 서버 내 자바 스크립트 요청입니다 응답을 얻지 못했습니다. 미리 감사드립니다.

+0

jQuery 덕분에 이제 내장 JavaScript 클래스를 어떻게 처리해야할지 모릅니다. 원시 클래스에서는 크로스 브라우저를 사용합니다. 하하. –

+0

브라우저는 개발자 도구 (보통 'F12','Ctrl + Shift + J' 또는'Command + Shift + J'로 열림)에서 오류를 발견합니까? 이 요청을하는 페이지가'http : // localhost : 8080 /'에서도 제공됩니까? –

답변

0

현재 XMLHttpRequest 또는 ActiveXObject 그 자체를 displayMarkersdata 값으로 전달하는 것으로 보입니다.

// ... 
    return displayMarkers(mypostrequest); 

서버의 응답이 JSON 것이다 need to be parsed 있다면, 이는 그 responseText property에 저장됩니다.

// ... 
    return displayMarkers(JSON.parse(mypostrequest.responseText)); 

는이 요청을하는 페이지가 http://localhost:8080/에 있습니다 것으로 가정 수여. 다른 출처가 포함 된 경우 (또는 file://의 경우 원산지가없는 경우) more work is required to allow the request.

+0

응답은 응답 텍스트 속성이어야하지만 비어 있어야합니다. – user3034457

관련 문제