Bilal lilla
Bilal lilla

Reputation: 658

Jquery unable to get the response from WCF REST service

I have developed WCF rest service and deployed it on a link that can be accessed via the browser because its action is "GET".

I want to get that data using jQuery. I tried my best to get WCf get response using jQuery but in vain. I also tried $.Ajax with 'jsonp' with no luck. Can any one help me?

The url is: http://www.lonestarus.com/AndroidApp/AndroidLocation.svc/RestService/getLatestLocation

You can check that url response by pasting url in browser.

Upvotes: 0

Views: 1158

Answers (3)

frictionlesspulley
frictionlesspulley

Reputation: 12358

You need to set Access-Control-Allow-Origin to value [*] in your response header.

this blog gives the more details how it can be done in WCF REST service

if you were to do this in Web API you could have just added

 Response.Headers.Add("Access-Control-Allow-Origin", "*")

calling the service using a fiddle

$(function() {

    $.ajax({
        url: "http://www.lonestarus.com/AndroidApp/AndroidLocation.svc/RestService/getLatestLocation",
        datatype: 'json',
        type : 'get',
        success: function(data) {
            debugger;

            var obj = data;
        }

    });

})​;​

I got the error

XMLHttpRequest cannot load http://www.lonestarus.com/AndroidApp/AndroidLocation.svc/RestService/getLatestLocation. Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin.

Upvotes: 1

malkassem
malkassem

Reputation: 1957

Sample code below:

$.ajax
(
    {
        type: 'GET',
        url: http://www.lonestarus.com/AndroidApp/AndroidLocation.svc/RestService/getLatestLocation,
        cache: false,
        async: true,
        dataType: 'json',
        success: function (response, type, xhr)
        {
            window.alert(response);
        },
        error: function (xhr)
        {
            window.alert('error: ' + xhr.statusText);
        }
    }
);

Upvotes: 0

ryan
ryan

Reputation: 6655

I can't make a cross domain example to show you but

$('#a').load('http://www.lonestarus.com/AndroidApp/AndroidLocation.svc/RestService/getLatestLocation​​​​​​​​​​​​​​​​​?callback=run');​

would work had those things been set.

Your service needs to either enable JSONP callbacks or set the Access-Control-Allow-Origin header for cross domain requests to work, or you need to run the script from the same domain. Given that your url says AndroidApp I'm thinking you want cross domain.

Upvotes: 1

Related Questions