0

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Bilal lilla
  • 618
  • 3
  • 8
  • 29

3 Answers3

1

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.

ryan
  • 6,541
  • 5
  • 43
  • 68
1

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.

frictionlesspulley
  • 11,070
  • 14
  • 66
  • 115
0

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);
        }
    }
);
malkassem
  • 1,937
  • 1
  • 20
  • 39
  • I went ahead and changed it. Although, I do need it to be true in my app, so it really depends on what you really need. – malkassem Dec 28 '12 at 18:35
  • i just need to data inside the successfunction. and your code is not working giving error 'Error no transport' – Bilal lilla Dec 28 '12 at 18:37