简体   繁体   English

Jquery无法从WCF REST服务获得响应

[英]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". 我已经开发了WCF休息服务并将其部署在可以通过浏览器访问的链接上,因为它的操作是“GET”。

I want to get that data using jQuery. 我想用jQuery获取数据。 I tried my best to get WCf get response using jQuery but in vain. 我尽力让WCf使用jQuery获得响应但是徒劳无功。 I also tried $.Ajax with 'jsonp' with no luck. 我也试过$ .Ajax和'jsonp'没有运气。 Can any one help me? 谁能帮我?

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

You can check that url response by pasting url in browser. 您可以通过在浏览器中粘贴网址来检查该网址响应。

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. 您的服务需要启用JSONP回调或设置Access-Control-Allow-Origin标头以使跨域请求生效,或者您需要从同一域运行脚本。 Given that your url says AndroidApp I'm thinking you want cross domain. 鉴于您的网址说AndroidApp,我认为您想要跨域。

You need to set Access-Control-Allow-Origin to value [*] in your response header. 您需要在响应标头中将Access-Control-Allow-Origin设置为值[*]。

this blog gives the more details how it can be done in WCF REST service 此博客提供了有关如何在WCF REST服务中完成的更多详细信息

if you were to do this in Web API you could have just added 如果您要在Web API中执行此操作,则可以添加

 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 . XMLHttpRequest无法加载http://www.lonestarus.com/AndroidApp/AndroidLocation.svc/RestService/getLatestLocation Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin. Access-Control-Allow-Origin不允许使用来源http://fiddle.jshell.net

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM