无法使用javascript使用WCF服务(readyState从1变为4)

本文关键字:readyState 变为 服务 javascript WCF 使用 | 更新日期: 2023-09-27 18:03:24

每当我尝试在外部文件中使用下面的javascript使用WCF时,readyState从1变为4。ResponsText在任何情况下都是空的。

每当我尝试使用相同的代码使用WCF时,但这次是在项目本身中,它就像它应该的那样工作。

我做错了什么?提前谢谢。

我有一个接口

namespace CeviService
{
     [ServiceContract(Namespace = "CeviService")]
    public interface ICeviSpotter
    {
         [WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
          String EchoWithPost(string n1, string n2);
  }
}

与实现

namespace CeviService
{
public class CeviSpotter : ICeviSpotter
{
     public String EchoWithPost(String n1, String n2)
    {
        return n1;
    }
}}
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"
%>

无法使用javascript使用WCF服务(readyState从1变为4)

如果包含JavaScript的页面没有从http://localhost:49456(包括端口号)提供服务,问题是您正在尝试进行跨域调用,这被同源策略阻止。例如,如果你在文件浏览器中双击打开的文件中使用JavaScript(所以它的协议是file://),或者你从http://localhost加载它(不使用端口49456),等等。

您可以通过处理飞行前呼叫并响应适当的标头(如果您使用支持CORS的浏览器)来启用跨域资源共享访问