简体   繁体   English

带WCF Rest Web服务的Ajax请求

[英]ajax request with wcf rest web service

I want to call web service from other website and get a response back in a html page using an Ajax call. 我想从其他网站调用Web服务,并使用Ajax调用在html页面中获得响应。

The data should be a response of a webservice. 数据应该是Web服务的响应。 I've tried a lot but I'm not getting any solution. 我已经尝试了很多,但是没有任何解决方案。 I also crossed browser, the issue is there when calling the webservice using the ajax request. 我还浏览器,使用ajax请求调用Web服务时存在问题。

My web service code: 我的网络服务代码:

    [ServiceContract]
    public interface IService
    {

        [WebInvoke(Method = "GET",RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        //[WebGet]
        [OperationContract]
        string GetData(string inputString); 
    }

It is implemented as:

    public string GetData(string inputString)
        {
            return "myData";
        }


Javascript code (From my other website):

 <script type="text/javascript">
            function click_btn() {
                $.ajax({
                    type: "GET",
                    url: "http://192.168.15.213/MyService.svc/GetData/inputString=e",
                    contentType: "application/json",
                    success: function (jsonData) {
                        alert('Data: ' + jsonData);
                 `enter code here`   },
                    Error: function (e) {
                        alert('err: ' + e);
                    }
                });
                return false;
            }
    </script>

I am getting the error 404 method not allowed when I call the webservice from another website. 当我从另一个网站调用Web服务时,出现错误404 method not allowed

That is correct, you cannot perform a request from different domains from your client context. 没错,您无法从客户端上下文执行来自其他域的请求。 But what you can do is fetch the data from the external website in your own server code, and then provide it as a service for your client purposes. 但是,您可以做的是使用自己的服务器代码从外部网站获取数据,然后将其作为服务提供给客户。 In fact, usually you need to give some context to data from external sources, so anyway you have to process that information before sending it to the client. 实际上,通常您需要为来自外部源的数据提供一些上下文,因此无论如何,您都必须先处理该信息,然后再将其发送给客户端。

hope it helps 希望能帮助到你

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

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