2010-11-15 19 views
1

UPDATE1:如何調用跨域的WebService/WCF在JQuery中

這裏是當我複製並粘貼到IE瀏覽器的服務URL是什麼我得到:

的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:

我看到服務正在恢復我作爲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 
           } 
          }); 
} 
+0

[訪問被拒絕使用jquery-1.4.1.js](http://stackoverflow.com/questions/4146650/access-is-denied-using-jquery-1-4-1-js) – lonesomeday 2010-11-15 19:29:46

+0

請修改現有問題,而不是在發佈相同問題時發佈新問題。 – lonesomeday 2010-11-15 19:30:11

+0

我不會在發佈的問題中獲取拒絕訪問錯誤。 – 2010-11-15 19:33:48

回答

0

我想「響應」是無效的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的工作方式是: 一個<script> - 元素將被注入到DOM,已設置的URL提供與src屬性。所以的ressource不是字符串或XML文檔,這是一個JavaScript文件。如果它被嵌入到文檔就會被執行,理由是可訪問的。

我不能告訴你創建jsonp的最終方式,它取決於你從哪裏獲得數據以及你喜歡用它來做什麼(當然也包括給定的環境)。

+0

我已經看過服務和特定的方法,我正在嘗試稱其返回「長」只是幾個號碼 – 2010-11-15 19:38:54

+0

我已更新我的問題... – 2010-11-15 19:49:56

+0

我明白了。您必須檢查服務器是否也可用jsonp格式。如果沒有,您需要創建一種代理(在服務器端),從遠程服務器獲取響應並將其轉發給html文檔。 – 2010-11-16 02:44:47

0

正常的.Net擴展WCF服務是.SVC它看起來像所有來電都.svs

我不認爲.svs是正常的.NET擴展,使運行時可能不是連試圖執行調用服務器端。

更改擴展或重新配置您的Web服務器。

+0

我的錯誤,這是錯字,我已糾正爲svc – 2010-11-15 19:44:45

0

說實話我不知道asp。淨,但我想你錯過了逃離這個 sb.AppendLine("<kml xmlns=\"someSITE">");,它應該是這樣的:sb.AppendLine("<kml xmlns=\"someSITE\">");

你可以從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(); 
     } 
+0

看起來像我搞砸了,當我改變網站名稱,所以我不認爲這是問題,爲什麼我的jquery沒有執行。 – 2010-11-15 19:56:26

1

我嘗試了多種不同的方式來執行下面的跨域參考,但沒有成功......

瀏覽器不允許因安全隱患跨域Ajax請求。如果您的網頁和Web服務位於不同的域中,則需要在與您的網頁相同的域中添加第二個頁面,以便將請求中介。

+0

我打算通過html頁面訪問wcf服務,所以我沒有任何網頁。 – 2010-11-15 21:06:41

+0

我在我的答案中同義地使用了html頁面和網頁。 wcf服務與html頁面位於同一個域中嗎? – roto 2010-11-15 22:05:31

+0

wcf服務在不同的服務器上,我試圖從本地開發機器訪問。 – 2010-11-17 18:45:39

0

嚴格禁止XMLHttpRequest對象在最初服務頁面的域之外調用Web服務。這有很多安全原因。

在你的情況下,服務該頁面的域是「本地主機」。 javascript無法訪問遠程web服務。你不會解決這個問題。

也就是說,你有一個選項。你可以讓你的JavaScript碰到一個LOCAL網絡服務,它會轉向並調用REMOTE網絡服務並將信息傳回給你的javascript。欲瞭解更多信息,請參閱this article on Simple-Talk,其中詳細介紹了幾種可用的選項。