LB.
LB.

Reputation: 14112

jQuery Ajax Problem

I'm trying to figure out what I am doing wrong here. Basically I'm trying to get a successful callback from:

http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer/project?f=pjson&inSR=4326&outSR=102113&geometries={"geometryType":"esriGeometryPoint","geometries":[{"x":-117,"y":34}]}

When pasting this into a browser, I get data, when I'm doing this in Javascript it doesn't work as it calls the error handler. Any ideas? I've used fiddler to compare the requests and found really no difference.

$.ajax({
    type: 'GET',
    url: 'http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer/project?f=pjson&inSR=4326&outSR=102113&geometries={"geometryType":"esriGeometryPoint","geometries":[{"x":-117,"y":34}]}',
    success:    function(data){ 
        alert('success');
    },
    error: function(jqXHR, textStatus, errorThrown){
        alert('error');
    }   
});

Upvotes: -1

Views: 785

Answers (5)

John Boker
John Boker

Reputation: 83699

You could probably use http://api.jquery.com/jQuery.getJSON/ and set your jsonp callback

$.getJSON('http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer/project?f=pjson&callback=?&inSR=4326&outSR=102113&geometries={"geometryType":"esriGeometryPoint","geometries":[{"x":-117,"y":34}]}', function(res)
{

});

something like that might work, notice the &callback=? in the query string that was added.

Upvotes: 0

Chris May
Chris May

Reputation: 822

I think I may have had the same problem. From my perspective, it seems ESRI has a funny way of understanding status codes.

The issue is that you received a 200 back from the server, which is supposed to signify a success. However, the transmission you are receiving from ESRI is an error. What I have to do with my ESRI ajax calls is to parse the response...

parse: function(response) {
    if (response.error) { //If there is a server error, it will find it here.
        this.searchError(response.error); //I would then send to the error function.
    }
    return response.features
}

Upvotes: 0

justkt
justkt

Reputation: 14766

Unless your website is at sampleserver1.arcgisonline.com, you are running into cross-site scripting concerns.

To mitigate them you can:

  1. Run your request through a proxy on your own server to your site.
  2. Use JSPONP, if the domain you are requesting information from supports it.

Upvotes: 1

Mārtiņš Briedis
Mārtiņš Briedis

Reputation: 17752

Add a jsonp paremeter:

$.ajax({
    type: 'GET',
    dataType : 'jsonp',
    url: 'http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer/project?f=pjson&inSR=4326&outSR=102113&geometries={"geometryType":"esriGeometryPoint","geometries":[{"x":-117,"y":34}]}',
    success:    function(data){ 
        alert('success');
    },
    error: function(jqXHR, textStatus, errorThrown){
        alert('error');
    }   
});

Upvotes: 3

kgiannakakis
kgiannakakis

Reputation: 104168

You need to make a jsonp request. In jQuery 1.5 you can set crossDomain to true.

Upvotes: 1

Related Questions