下午好。我并不是要碰这个线程,但是已经过了一个多月,而且我似乎无法获得任何职位或获得工作请求。我已经看过(并尝试合并)这么多预先编写的解决方案,但是每一次似乎都出现了我无法解决的问题。任何帮助将不胜感激。非常感谢。

解决我的问题:我一直在尝试使用简单的“ Hello消息”创建默认的WCF服务,该服务根据Filburt的建议接受POST数据,而我什么也做不了。任何帮助,将不胜感激。我正在尝试为CRM 2013编写一个测试Web服务,该服务将从窗体上的iframe触发,并调用位于同一crm服务器上的启用AJAX的WCF Web服务。该消息将需要向该服务发送POST(未获取)消息。该服务似乎返回“ 400”错误:“ XMLHttpRequest无法加载http://localhost/PasswordResetter/Resetter.svc。无效的HTTP状态代码400”。我已经尝试浏览了30多个左右的“教程”以及此处的说明,Web,msdn,但我不知道我似乎在做错什么,或者为什么它不起作用。我无法从iframe或自定义html页面(我创建的服务和html页面都位于本地主机)上运行。

最后,如果此问题在某种程度上是“此网站的格式不正确”,请向我指出如何/为什么这样做,以便我对其进行适当的修改。预先感谢您的所有帮助。

下面是我的界面:

namespace PasswordResetter
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IResetter" in both code and config file together.
    [ServiceContract]
    public interface IResetter
    {
        [OperationContract()]
        string test(string a, string b);
    }
}


方法如下:

public class Resetter : IResetter
{
    [WebInvoke(Method = "POST", //was POST
    BodyStyle = WebMessageBodyStyle.Bare, //This means that our JSOn will map directly to our parameters with no additional processing??
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "/Test")]
    public string test(string a, string b)
    {
        //return "Success!: " + a + " " + b;
        string myJsonString = (new JavaScriptSerializer()).Serialize("Hello World! A: " + a + " b: " + b);
        return myJsonString;
    }
}


这是我正在尝试使用的Javascript:

$(document).ready(function () {
    $('#iframesubmit').click(function () {
        //var obj = { username: $("#txtuser").val(), name: $("#txtname").val() };
        var obj = '[{ username: $("#password_old").val(), name: $("#password_new").val() }]';
        $.ajax({
            type: "POST", //was POST
            contentType: "application/json; charset=utf-8",
            url: "http://localhost/PasswordResetter/Resetter.svc",
            //data: "",
            data: JSON.stringify(obj),
            //data: "{ 'a': '" + $("#password_old").val() + "', 'b': '" + $("#password_new").val() + "'}",//JSON.stringify(obj),
            dataType: "json",
            success: function (data) {
                alert("Successfully register");
                document.getElementById("results").value = "Response: " + data; //service.responseText;
                $("#iframesubmit").click();
            },error: function (xhr)
            {
                window.alert('error: ' + xhr.statusText);
            }
        });
    });
});


最后是我的web.config:

<?xml version="1.0"?> <configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5"/> </system.web> <system.serviceModel>
<bindings> <webHttpBinding> <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true"> <security mode="Transport"> <transport clientCredentialType="None"></transport> </security> </binding> </webHttpBinding> <customBinding> <binding name="CRMPoint.CRM.Services.PasswordResetter.customBinding0"> <!-- <binaryMessageEncoding /> --> <httpTransport /> </binding> </customBinding> </bindings> <services> <service name="PasswordResetter"> <endpoint address="http://localhost/PasswordResetter/Resetter.svc" behaviorConfiguration="PasswordResetter.Service1AspNetAjaxBehavior" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="PasswordResetter.IResetter" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> <service name="Test"> <endpoint address="http://localhost/PasswordResetter/Resetter.svc" binding="customBinding" bindingConfiguration="CRMPoint.CRM.Services.PasswordResetter.customBinding0" contract="PasswordResetter" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> <service name="PasswordResetter.Service1"> <endpoint address="http://localhost/PasswordResetter/Resetter.svc" behaviorConfiguration="PasswordResetter.Service1AspNetAjaxBehavior" binding="webHttpBinding" contract="PasswordResetter.Service1" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <endpointBehaviors> <behavior name="webHttpBehavior"> <webHttp helpEnabled="true" /> </behavior> <behavior name="PasswordResetter.Service1AspNetAjaxBehavior"> <enableWebScript /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> <standardEndpoints> <webScriptEndpoint> <standardEndpoint name="test" crossDomainScriptAccessEnabled="true" /> </webScriptEndpoint> </standardEndpoints> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="Content-Type" /> </customHeaders> </httpProtocol> <directoryBrowse enabled="true"/> </system.webServer> </configuration>

最佳答案

代码中有一些问题...


首先,使用WebInvoke修饰方法通常是在
接口不在主类中
其次是方法名称(test),而UriTemplate="/Test"应该是
相同。应该如下
UriTemplate="/Test"
 public string Test(string a, string b){ string myJsonString = (new JavaScriptSerializer()).Serialize("Hello World! A: " + a + " b: " + b); return myJsonString;}
在您对此方法进行ajax调用时出现问题
替换以下行


  网址:“ http://localhost/PasswordResetter/Resetter.svc
  与
  网址:“ http://localhost/PasswordResetter/Resetter.svc/Test
  
  希望这会有所帮助。

09-25 21:13