0

I can´t get the response from an object XMLHttpRequest as a result of a REST call. I know that results are inside the object cause the object size is bigger when I have results to return but I can´t access to them anyway. This is my javascript request to the server:

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
}

And finally this is the response of my Java web service:

@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;
}

I tried as well using jquery but I have the same problem, I can´t get any response. Thanks in advance.

user3034457
  • 37
  • 1
  • 2
  • 8
  • Thanks to jQuery, now I don't know what to do the built-in JavaScript classes (and do it cross-browser in raw types). Ha ha. – Paul Vargas Nov 26 '13 at 00:50
  • Does your browser note any errors in its developer tools (usually opened with `F12`, `Ctrl+Shift+J` or `Command+Shift+J`)? Is the page making this request also served from `http://localhost:8080/`? – Jonathan Lonowski Nov 26 '13 at 01:01

1 Answers1

0

It appears you're currently passing the XMLHttpRequest or ActiveXObject itself to displayMarkers as the value of data.

// ...
    return displayMarkers(mypostrequest);

The response from the server will be stored in its responseText property, which will need to be parsed if it's JSON.

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

Granted, that assumes that the page making this request is also on http://localhost:8080/. If another origin is involved (or lack of origin in the case of file://), then more work is required to allow the request.

Community
  • 1
  • 1
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199