GET工作正常。 但在使用POST方法的Chrome中我有: 无法加载资源:服务器响应状态为405(Method Not Allowed)。 无法加载http://localhost:49834/api/Rest/:预检的响应具有无效的HTTP状态代码405。 在InterntExplorer我有:SCRIPT7002:XMLHttpRequest:网络错误0x80070005,访问被拒绝。
控制器:
[EnableCors("*", "*", "*")]
public class RestController : ApiController
{
// POST: api/Rest
[HttpPost]
public void Post([FromBody]Request Request)
{
if (ModelState.IsValid)
{
db.Requests.Add(Request);
db.SaveChanges();
}
}
}
WebApiConfig.cs
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
的Web.config:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
</customHeaders>
</httpProtocol>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/> <!-- add this -->
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="WebDAV" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
AJAX POST:
var url = "http://localhost:49834/";
$.ajax({
type: "POST",
url: url + "api/Rest/",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(submitData),
success: function (data) {
},
error: function (err) {
console.log(err);
alert(err.status + " - " + err.statusText);
}
})
我在%userprofile%\ documents \ IISExpress \ config folder =&gt;中使用IIS Express:applicationHost.config文件ExtensionlessUrl-Integrated-4.0处理程序配置如此动词=“GET,HEAD,POST,DEBUG,PUT,DELETE”
GET工作正常:
$.getJSON(url + "api/Rest").done(function (data) {
var mass = new Array();
$(data).each(function (index, e) {
ReqArray.push(e);
})
vm.requests(ReqArray);
});
答案 0 :(得分:0)
我的ajax存在一些问题,就像这样效果很好:
var url = "http://localhost:49834/";
$.ajax({
type: "POST",
url: url + "api/Rest",
dataType: "json",
data: submitData,
success: function (data) {
},
error: function (err) {
console.log(err);
alert(err.status + " - " + err.statusText);
}
})