每当我尝试在外部文件中使用下面的 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"
%>