jQuery无法从WCF REST服务获取响应 [英] Jquery unable to get the response from WCF REST service

查看:125
本文介绍了jQuery无法从WCF REST服务获取响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了WCF rest服务并将其部署在可以通过浏览器访问的链接上,因为它的动作是"GET".

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

我想使用jQuery获得该数据.我尽力使用jQuery获得WCf响应 但徒劳无功.我也尝试了$ .Ajax和'jsonp',但是没有运气.有人可以帮我吗?

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?

网址为: http://www.lonestarus.com/AndroidApp/AndroidLocation.svc/RestService/getLatestLocation

您可以通过将URL粘贴到浏览器中来检查该URL响应.

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

推荐答案

您需要在响应标题中将 Access-Control-Allow-Origin 设置为[*]值.

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

如果要在Web API中执行此操作,则可以添加

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

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

使用小提琴拨打服务

$(function() {

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

            var obj = data;
        }

    });

})​;​

我得到了错误

XMLHttpRequest无法加载 http://www.lonestarus.com/AndroidApp/AndroidLocation.svc/RestService/getLatestLocation . 不允许 http://fiddle.jshell.net 访问控制允许来源.

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.

这篇关于jQuery无法从WCF REST服务获取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆