简体   繁体   English

通过jQuery调用JSON wcf服务

[英]Call JSON wcf service via jQuery

I have question according calling json wcf methods from aspx page with jQuery. 我有一个问题,用jQuery从aspx页面调用json wcf方法。

This is my test method: 这是我的测试方法:

    [ServiceContract]
    public interface IEParcelService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "Test")]
        Response<string> Test();
    }

  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  public class EParcelServiceImpl : IEParcelService
  {    
    public Response<string> Test()
    {
      return WebServiceHelper.Execute(() => "Test Message");
    }    
  }

This service deployed to IIS. 此服务已部署到IIS。 When I call this method from Chrome: http://localhost/TestService/ServiceImpl.svc/Test Everything is ok and I can see the result. 当我从Chrome调用此方法时: http://localhost/TestService/ServiceImpl.svc/Test一切正常,我可以看到结果。 But when I call it from jQuery I have error: NETWORK_ERR: XMLHttpRequest Exception 101. I try to find solution in Google. 但是,当我从jQuery调用它时,出现错误:NETWORK_ERR:XMLHttpRequest异常101。我尝试在Google中找到解决方案。 But the result was not successful. 但是结果并不成功。 How I can solve it? 我该如何解决?

jQuery call: jQuery呼叫:

<script language="javascript">
  $().ready(function () {
            $("#myButt").click(function () {

                $.ajax({
                    cache: false,
                    async: false,
                    type: "GET",
                    url: "http://localhost/EParselService/EParcelServiceImpl.svc/Test",
                    contentType: "application/json",
                    dataType: "json",
                  success: function (data, textStatus){
                                alert('successCallBack called');
                                alert(data);
                                alert(textStatus);
                         },
                   error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert('errorCallBack called');
                            alert('textStatus = ' + textStatus + '/nerrorThrown = ' + errorThrown);
                        } 
                 });
                alert('Done!');
            });
        });
</script>

<input type="button" value="Get values from server" id="myButt" />

So it's a same-origin issue. 因此,这是一个同源问题。 (see here ) (请参阅此处

For a XMLHttpRequest the resource has to be on the exact same domain as the page requesting it. 对于XMLHttpRequest,资源必须与请求它的页面在完全相同的域上。 This is to prevent XSS (see here ) atacks. 这是为了防止XSS(请参阅此处 )攻击。 If you want to use a resource from a different domain you'll have to use something like jsonp. 如果要使用其他域中的资源,则必须使用jsonp之类的东西。 (see here ) For a good tutorial how to do this with WCF. (请参阅此处 )有关如何使用WCF进行操作的良好教程。

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

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