Farkas
Farkas

Reputation: 11

http-Get request with empty response

I want to process the response of a http-request with JavaScript. You can find a simple example here.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
  <title>JavaScript Test</title>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <script type="text/javascript">
    function serviceCall() {
      $.ajax({
        type: "GET",
        url: 'http://localhost:8181/geoserver/wfs?Service=WFS&Request=GetFeature&Version=1.0.0&typename=topp:tasmania_water_bodies&SRS=EPSG:4326',
//        url: 'http://demo.opengeo.org/geoserver/ows?Service=WFS&Request=GetFeature&Version=1.0.0&typename=topp:tasmania_water_bodies&SRS=EPSG:4326',
        complete: function(xml, status){
          alert(xml.responseText);
        }
      });
    }
  </script>
</head>
<body>
  <center><button onclick="serviceCall()">Start...</button></center>
</body>
</html>

The request works directly in a browser. Via Ajax and JavaScript the response is empty. Firebug reports a xml parsing error at line 1, column 1. I've tried to send the request to localhost and to a remote server but the response is always empty. I would appreciate any advice.

Upvotes: 1

Views: 3148

Answers (1)

Niels
Niels

Reputation: 49919

Why don't you use success instead of complete? Since complete is fired always, even if it failed, and success only if it was successfull. Than you don't need the xml, status.

Example (not working since CORS):

$.ajax({
    type: "GET",
    url: 'http://localhost:8181/geoserver/wfs?Service=WFS&Request=GetFeature&Version=1.0.0&typename=topp:tasmania_water_bodies&SRS=EPSG:4326',
    success: function(response){
      alert(response);
    }
  });

Also if you wan't to access a different domain. You can use JSONP if you own the other domain. Otherwise it is not possible.

Try to add the following part to the URL: &outputFormat=json&format_options=callback:processJSON

Working None jQuery example (LIVE EXAMPLE HERE: http://jsfiddle.net/QWgJa/)

function loadJSON(url)
{
    var headID = document.getElementsByTagName("head")[0]; 
    var newScript = document.createElement("script");
    newScript.type = 'text/javascript';
    newScript.src = url;
    headID.appendChild(newScript);  
}
function processJSON(jsonData)
{
    alert(jsonData); 
}

loadJSON("http://demo.opengeo.org/geoserver/ows?Service=WFS&Request=GetFeature&Version=1.0.0&typename=topp:tasmania_water_bodies&SRS=EPSG:4326&outputFormat=json&format_options=callback:processJSON");

Information URLs

Upvotes: 1

Related Questions