2013-03-12 35 views
0

每當我嘗試在外部文件中使用下面的javascript來使用WCF時,readyState從1變爲4. ResponsText在每種情況下始終爲空。無法使用JavaScript來使用WCF服務(readyState從1到4)

每當我嘗試使用相同的代碼來使用WCF,但是這次是在項目本身中,它就像它應該的那樣工作。

我在做什麼錯?提前致謝。

我有一個實現

namespace CeviService 
{ 
public class CeviSpotter : ICeviSpotter 
{ 
    public String EchoWithPost(String n1, String n2) 
    { 
     return n1; 
    } 
}} 

接口

namespace CeviService 
{ 
    [ServiceContract(Namespace = "CeviService")] 
    public interface ICeviSpotter 
    { 

     [WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] 
      String EchoWithPost(string n1, string n2); 
    } 

} 

使用下面的web.config

<?xml version="1.0"?> 
<configuration> 
    <system.serviceModel> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior name="MyServiceTypeBehaviors"> 
        <serviceMetadata httpGetEnabled="true"/> 
       </behavior> 
      </serviceBehaviors> 
      <endpointBehaviors> 
       <behavior name="AjaxBehavior"> 
        <webHttp/> 
       </behavior> 
      </endpointBehaviors> 
     </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
     <services> 
      <service name="CeviService.CeviSpotter" behaviorConfiguration="MyServiceTypeBehaviors"> 
       <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/> 
       <endpoint address="ajaxEndpoint" behaviorConfiguration="AjaxBehavior" binding="webHttpBinding" contract="CeviService.ICeviSpotter"/> 
      </service> 
     </services> 
    </system.serviceModel> 
    <system.web> 
     <compilation debug="true"/></system.web> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
</configuration> 

正在使用下面的JavaScript調用:

function makeCall(operation) { 

     // Create HTTP request 
     var xmlHttp; 
     try { 
      xmlHttp = new XMLHttpRequest(); 
     } catch (e) { 
      try { 
       xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
      } catch (e) { 
       try { 
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
       } catch (e) { 
        alert("This sample only works in browsers with AJAX support"); 
        return false; 
       } 
      } 
     } 

     // Create result handler 
     xmlHttp.onreadystatechange = function() { 
      alert(xmlHttp.readyState); 
       // alert(xmlHttp.responseText); 

     } 

     // Build the operation URL 
     var url = "http://localhost:49456/CeviSpotter.svc/ajaxendpoint/EchoWithPost"; 
     //url = url + operation; 

     // Build the body of the JSON message 
     var val1 = document.getElementById("num1").value; 
     var val2 = document.getElementById("num2").value; 


     var body = '{"n1":'; 
     body = body + val1 + ',"n2":'; 
     body = body + val2 + '}'; 

     // Send the HTTP request 
     xmlHttp.open("POST", url, true); 
     xmlHttp.setRequestHeader("Content-type", "application/json"); 
     xmlHttp.send(body); 

    } 

和主機如下配置:

<%@ServiceHost 
    language="C#" 
    Debug="true" 
    Service="CeviService.CeviSpotter" 
%> 

回答

0

如果不從http://localhost:49456服務包含JavaScript的頁面(包括端口號),問題是,你想使一個跨網來電,被Same Origin Policy阻止。例如,如果您在通過在文件資源管理器中雙擊打開的文件中使用該JavaScript(因此其協議爲file://),或者您從http://localhost(不使用端口49456)加載該文件,等。

您可以通過使用Cross-Origin Resource Sharing通過處理預先航班呼叫並使用相應的標題(只要您使用的是browser that supports CORS)進行響應來啓用訪問。

+0

謝謝TJ。有沒有解決這個問題的方法?我們如何解決這個問題? WCF服務應該在主機上運行,​​以便移動應用程序可以訪問它。 編輯:交叉Oroigin資源共享工作的預期用途? – 2013-03-12 10:57:54

+0

@JensCocquyt:正如我在答案中提到的,CORS是一個選項。如果CORS不是一種選擇,我對WCF服務瞭解不多,但如果它產生的格式可以是多種格式的,你可以看看[JSON-P](http://en.wikipedia.org /維基/ JSONP#JSONP)。 – 2013-03-12 11:00:09

+0

謝謝T.J.克勞德。我們確實能夠用CORS修復它。幫助我的信息:[CORS on client](http://www.html5rocks.com/en/tutorials/cors/)[wcf服務中的CORS](http://blog.weareon.net/calling-wcf-rest -service-from-jquery-causes-405-method-not-allowed /) – 2013-03-12 15:34:17