如何在JQuery中调用跨域WebService/WCF

发布于 2024-10-02 09:31:02 字数 4268 浏览 0 评论 0原文

更新1:

这是我在 IE 浏览器中复制并粘贴服务网址时得到的结果:

http://myservername/myservices.svc?wsdl

- <wsdl:message name="ILodge_GetCountfor">
  <wsdl:part name="parameters" element="tns:GetCountfor" /> 
  </wsdl:message>
- <wsdl:message name="ILodge_GetCountfore">
  <wsdl:part name="parameters" element="tns:GetCountfor" /> 
  </wsdl:message>
- <wsdl:message name="ILodge_GetCountfor_Input">
  <wsdl:part name="parameters" element="tns:GetCountfor" /> 
  </wsdl:message>
- <wsdl:message name="ILodge_GetCountfor">
  <wsdl:part name="parameters" element="tns:GetCountfor" /> 
  </wsdl:message>

http ://myservername/myservices.svc?xsd=xsd0

- <xs:element name="GetCountfor">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="GetCountforResult" type="xs:long" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="GetCountfor">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="Id" nillable="true" type="xs:string" /> 
  <xs:element minOccurs="0" name="LevelId" type="xs:long" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="GetCountfor">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="GetCountfor" type="xs:long" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>

更新:

我看到服务以 XML 形式返回我:

 public override string ToString()
    {            
        //- <name>CLUE</name><desc>CLUE list</desc> 
        StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
        sb.AppendLine("<kml xmlns=\"someSITE">");
        sb.AppendLine("<FOLDER>");
        sb.AppendLine("<name>Clue</name>");
        sb.AppendLine("<desc>Clue list</desc>");
        sb.AppendLine("</FOLDER>");
        sb.AppendLine("</kml>");
        return sb.ToString();
    }

我尝试了不同的方法来执行下面的跨域引用,但没有成功...我在这里做错了什么?我尝试调试并设置断点,但看起来它永远不会执行

 $(document).ready(function () {

$.getJSON("http://servername/tools/myservice.svc/mymethod/?Id=1&callback=?", null,           
      function (result) {
        alert("in test: " + result);
        debugger
        $("#spText").html(result);
    }); 

OR

        var path = "http://servername/tools/myservice.svc/mymethod?Id=1&callback=?";
             $.ajax({
                 type: "GET",
                 url: path,
                 contentType: "application/json; charset=utf-8",
                 dataType: "json",
                 async: false,
                 success: function (response) {
                    debugger
                     if (response != null) {
                         //displayData(response);
                     }
                 }
             });

OR
             debugger
             $.ajax({ url: "http://servername/tools/myservice.svc/mymethod",
                 data: { Id: "1" },
                 dataType: "jsonp",
                 success: function (json, textStatus) {
                     alert(json.d);
                     alert(textStatus);
                 },
                 error: function (XMLHttpRequest, textStatus, errorThrown) {
                     debugger
                 }
             });


    OR
             $.ajax({
                 type: "GET",
                cache: false,
                url: "http://servername/tools/myservice.svc/mymethod/Id=1&callback=?",
                              scriptCharset: "utf-8",
                              dataType: "jsonp",
                              data: parameters,
                              success: function (data, textStatus) {
                                  debugger
                              },
                              error: function (XMLHttpRequest, textStatus, errorThrown) {
                                  debugger
                              }
                          });
}

UPDATE1:

here is what i am getting when i copy and paste the service url in the IE browser:

http://myservername/myservices.svc?wsdl

- <wsdl:message name="ILodge_GetCountfor">
  <wsdl:part name="parameters" element="tns:GetCountfor" /> 
  </wsdl:message>
- <wsdl:message name="ILodge_GetCountfore">
  <wsdl:part name="parameters" element="tns:GetCountfor" /> 
  </wsdl:message>
- <wsdl:message name="ILodge_GetCountfor_Input">
  <wsdl:part name="parameters" element="tns:GetCountfor" /> 
  </wsdl:message>
- <wsdl:message name="ILodge_GetCountfor">
  <wsdl:part name="parameters" element="tns:GetCountfor" /> 
  </wsdl:message>

http://myservername/myservices.svc?xsd=xsd0

- <xs:element name="GetCountfor">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="GetCountforResult" type="xs:long" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="GetCountfor">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="Id" nillable="true" type="xs:string" /> 
  <xs:element minOccurs="0" name="LevelId" type="xs:long" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="GetCountfor">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="GetCountfor" type="xs:long" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>

UPDATE:

i see the services is returning me as XML:

 public override string ToString()
    {            
        //- <name>CLUE</name><desc>CLUE list</desc> 
        StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
        sb.AppendLine("<kml xmlns=\"someSITE">");
        sb.AppendLine("<FOLDER>");
        sb.AppendLine("<name>Clue</name>");
        sb.AppendLine("<desc>Clue list</desc>");
        sb.AppendLine("</FOLDER>");
        sb.AppendLine("</kml>");
        return sb.ToString();
    }

i have tried different way to execute the below cross domain reference but no success... what i am doing wrong here? i try debugging and put break point but looks like it never execute

 $(document).ready(function () {

$.getJSON("http://servername/tools/myservice.svc/mymethod/?Id=1&callback=?", null,           
      function (result) {
        alert("in test: " + result);
        debugger
        $("#spText").html(result);
    }); 

OR

        var path = "http://servername/tools/myservice.svc/mymethod?Id=1&callback=?";
             $.ajax({
                 type: "GET",
                 url: path,
                 contentType: "application/json; charset=utf-8",
                 dataType: "json",
                 async: false,
                 success: function (response) {
                    debugger
                     if (response != null) {
                         //displayData(response);
                     }
                 }
             });

OR
             debugger
             $.ajax({ url: "http://servername/tools/myservice.svc/mymethod",
                 data: { Id: "1" },
                 dataType: "jsonp",
                 success: function (json, textStatus) {
                     alert(json.d);
                     alert(textStatus);
                 },
                 error: function (XMLHttpRequest, textStatus, errorThrown) {
                     debugger
                 }
             });


    OR
             $.ajax({
                 type: "GET",
                cache: false,
                url: "http://servername/tools/myservice.svc/mymethod/Id=1&callback=?",
                              scriptCharset: "utf-8",
                              dataType: "jsonp",
                              data: parameters,
                              success: function (data, textStatus) {
                                  debugger
                              },
                              error: function (XMLHttpRequest, textStatus, errorThrown) {
                                  debugger
                              }
                          });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

弃爱 2024-10-09 09:31:02

我尝试了不同的方式来执行下面的跨域引用,但没有成功...

由于安全风险,浏览器不允许跨域ajax请求。如果您的网页和 Web 服务位于不同的域中,您将需要在与您的网页相同的域中添加第二个页面来代理请求。

i have tried different way to execute the below cross domain reference but no success...

Browsers do not allow cross domain ajax requests due to the security risks. If your web page and web service are on different domains you will need to add a second page, on the same domain as your web page, to broker the requests.

梦途 2024-10-09 09:31:02

我猜“响应”不是有效的 jsonp。

将网址复制到浏览器的地址栏中,然后向我们展示您将看到的内容。

下面是 jsonp 的示例:

someFunction({'foo':'some foo','bar':'some bar'})

来自 flickr 的实例: http://www.flickr.com/services/rest/?method=flickr.test.echo&format=json&api_key=08e4f6fc4216b1216c5f521133ecbd9b&jsoncallback=functionName

看起来就像以对象文字作为参数调用函数一样。看起来像这样,因为稍后它将是一个函数调用。 jsonp 的工作方式是:

我无法告诉您创建 jsonp 的最终方法,这取决于您从哪里获取数据以及您喜欢用它做什么(当然也取决于给定的环境)。

I guess the "response" is not valid jsonp.

Copy the url into your browsers adressbar an show us what you'll see.

Here an example how jsonp looks like:

someFunction({'foo':'some foo','bar':'some bar'})

A live example from flickr: http://www.flickr.com/services/rest/?method=flickr.test.echo&format=json&api_key=08e4f6fc4216b1216c5f521133ecbd9b&jsoncallback=functionName

It looks like the call of a function with an object-literal as argument. It looks like that, because it will later be a function-call. The way jsonp works is:
A <script> -element will be injected into the DOM , which has set the provided URL as src-attribute. So the ressource is not a string or XML-document, it's a javascript-file. If it is embedded into the document it will be executed and the argument is accessible.

I can't tell you the ultimate way to create jsonp, it depends on where you get the data from and what you like to do with it(and of course on the given environment too).

夏日浅笑〃 2024-10-09 09:31:02

WCF 服务的正常 .Net 扩展名是 .svc,看起来您的所有调用都是对 .svs

我不认为 .svs 是正常的 .net 扩展名,因此运行时可能甚至不会尝试执行调用服务器端。

更改扩展名或重新配置您的网络服务器。

The normal .Net extension for WCF services is .svc it looks like all of your calls are to .svs

I don't think .svs is a normal .net extension so the runtime is probably not even trying to execute the call server side.

Change the extensions or reconfigure your web server.

┼── 2024-10-09 09:31:02

老实说我不了解asp.net,但我认为你错过了逃避这个问题
sb.AppendLine(""); ,它应该是这样的: sb.AppendLine("");

你可以从SO编辑器的突出显示功能中看出这一点,感谢SO团队:)

   public override string ToString()
        {            
            //- <name>CLUE</name><desc>CLUE list</desc> 
            StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            sb.AppendLine("<kml xmlns=\"someSITE\">");
            sb.AppendLine("<FOLDER>");
            sb.AppendLine("<name>Clue</name>");
            sb.AppendLine("<desc>Clue list</desc>");
            sb.AppendLine("</FOLDER>");
            sb.AppendLine("</kml>");
            return sb.ToString();
        }

to be honest i don't know about asp.net , but i think you missed to escape this
sb.AppendLine("<kml xmlns=\"someSITE">"); , it should be like : sb.AppendLine("<kml xmlns=\"someSITE\">");

you can tell it from the highlighting feature of SO editor , Thanks SO Team :)

   public override string ToString()
        {            
            //- <name>CLUE</name><desc>CLUE list</desc> 
            StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            sb.AppendLine("<kml xmlns=\"someSITE\">");
            sb.AppendLine("<FOLDER>");
            sb.AppendLine("<name>Clue</name>");
            sb.AppendLine("<desc>Clue list</desc>");
            sb.AppendLine("</FOLDER>");
            sb.AppendLine("</kml>");
            return sb.ToString();
        }
梦里南柯 2024-10-09 09:31:02

严格禁止 XMLHttpRequest 对象调用最初提供页面服务的域之外的 Web 服务。这有很大的安全原因。

在您的情况下,提供该页面的域是“localhost”。 JavaScript 无法访问远程 Web 服务。你无法回避这个问题。

也就是说,你确实有一个选择。您可以让您的 javascript 访问本地 Web 服务,该服务返回并调用远程 Web 服务并将信息传递回您的 javascript。有关详细信息,请参阅本文Simple-Talk 详细介绍了您可以使用的几种潜在选项。

The XMLHttpRequest object is strictly prohibited from calling web services outside of the domain that originally served the page. There are huge security reasons for this.

In your case the domain which served the page is "localhost". The javascript cannot access the remote webservice. You won't get around this.

That said, you do have an option. You can have your javascript hit a LOCAL web service which turns around and calls the REMOTE web service and passes the information back to your javascript. For more information see this article on Simple-Talk which goes into detail on several potential options available to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文