У меня есть следующий веб-сервис, созданный и использующий самую последнюю версию jquery, которую мне нужно отправить в веб-службу.Перекрестный домен AJAX Post - WCF
Я не могу понять это. Я читал, что JSONP не будет работать с POST. Как мне заставить это работать?
Мне нужно сделать перекрестный домен, используя jQuery для WCF.
service.cs:
namespace AjaxPost
{
[DataContractAttribute]
public class Message
{
[DataMemberAttribute]
public string success;
public Message(string success)
this.success = success;
}
[ServiceContract(Namespace="JsonpAjaxService")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class User
{
[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method="POST")]
public Message CreateUser(string email, string username, string password, string phone, string image)
{
Message msg = new Message("true");
return msg;
}
}
}
service.svc:
<%@ServiceHost
language="c#"
Debug="true"
Service="Microsoft.Samples.Jsonp.CustomerService"
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
%>
Сервис Web.Config:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE" />
</customHeaders>
</httpProtocol>
</system.webServer>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MetadataBehavior">
<serviceDebug includeExceptionDetailInFaults="True" httpHelpPageEnabled="True" />
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint name="" crossDomainScriptAccessEnabled="true"/>
</webScriptEndpoint>
</standardEndpoints>
</system.serviceModel>
index.html
wcfServiceUrl = "http://localhost:33695/Service.svc/CreateUser";
$.ajax({
crossDomain: true,
cache: true,
url: wcfServiceUrl,
data: "{}",
type: "POST",
jsonpCallback: "Message",
contentType: "application/json",
dataType: "json",
data: "{ \"myusername\": \"mypassword\" }",
error: function (request, status, error) {
//error loading data
alert("error");
},
success: function (menu) {
alert('success');
}
});
FYI, Edge, похоже, в настоящее время не поддерживает CORS ... если у меня что-то не так с моей реализацией этой ссылки, но она работает во всех других браузерах. – xr280xr