0

我想从其他网站调用 Web 服务并使用 Ajax 调用在 html 页面中获得响应。

数据应该是 Web 服务的响应。我已经尝试了很多,但我没有得到任何解决方案。我也穿过浏览器,使用 ajax 请求调用 web 服务时出现问题。

我的网络服务代码:

    [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>

404 method not allowed当我从另一个网站调用 web 服务时出现错误。

4

1 回答 1

0

没错,您无法从客户端上下文执行来自不同域的请求。但是您可以做的是在您自己的服务器代码中从外部网站获取数据,然后将其作为服务提供给您的客户。事实上,通常您需要为来自外部源的数据提供一些上下文,因此无论如何您必须在将这些信息发送给客户端之前对其进行处理。

希望能帮助到你

于 2012-12-10T12:48:29.357 回答