I'm getting this error:
我收到这个错误:
XMLHttpRequest cannot load http://apidev.facu.la/api/json/infotag/search/?criteria=&infotagType=book&companyid=2. Response for preflight has invalid HTTP status code 405 when trying to do this httpget call:
XMLHttpRequest无法加载http://apidev.facu.la/api/json/infotag/search/?criteria=&infotagType=book&companyid=2。尝试执行此httpget调用时,预检的响应具有无效的HTTP状态代码405:
$http({
method: 'GET',
dataType: "json",
url: 'http://apidev.facu.la/api/json/infotag/search/?criteria=&infotagType=book&companyid=2',
headers: {
"Content-Type": "application/json"
}
}).then(function successCallback(response) {
alert('ok');
}, function errorCallback(response) {
alert('error');
console.log(response);
});
Any idea what am I missing? Postman calls work just fine. Thanks
知道我错过了什么吗?邮差调用很好。谢谢
UPDATE:
Endpoint code:
[WebInvoke(Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/json/infotag/search/?criteria={criteria}&infotagType={infotagType}&companyid={companyid}")]
public XElement InfoTagSearchXML_json(string criteria, string infotagType, int companyid)
{
return InfoTagSearchXML(criteria, infotagType, companyid);
}
2 个解决方案
#1
0
You can add a WebInvoke for Method OPTIONS to your ServiceContract like this:
您可以将方法选项的WebInvoke添加到ServiceContract,如下所示:
[ServiceContract]
public interface ITestService
{
[WebInvoke(Method = "OPTIONS", UriTemplate = "*")]
void getOptions();
... other invokes
}
public class Service : ITestService
{
public void getOptions()
{
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "GET, OPTIONS");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type");
}
... other invokes
}
#2
0
Do the following:
请执行下列操作:
Install-Package Microsoft.AspNet.WebApi.Cors
then go to Web.config and remove these lines (if you have them)
然后转到Web.config并删除这些行(如果有的话)
<remove name="X-Powered-By" />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Accept, Content-Type, Origin" />
<add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, OPTIONS" />
and add the following code in WebApiConfig.cs file from App_Start folder:
并从App_Start文件夹中的WebApiConfig.cs文件中添加以下代码:
var enableCorsAttribute = new EnableCorsAttribute("*",
"Origin, Content-Type, Accept",
"GET, PUT, POST, DELETE, OPTIONS");
config.EnableCors(enableCorsAttribute);
#1
0
You can add a WebInvoke for Method OPTIONS to your ServiceContract like this:
您可以将方法选项的WebInvoke添加到ServiceContract,如下所示:
[ServiceContract]
public interface ITestService
{
[WebInvoke(Method = "OPTIONS", UriTemplate = "*")]
void getOptions();
... other invokes
}
public class Service : ITestService
{
public void getOptions()
{
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "GET, OPTIONS");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type");
}
... other invokes
}
#2
0
Do the following:
请执行下列操作:
Install-Package Microsoft.AspNet.WebApi.Cors
then go to Web.config and remove these lines (if you have them)
然后转到Web.config并删除这些行(如果有的话)
<remove name="X-Powered-By" />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Accept, Content-Type, Origin" />
<add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, OPTIONS" />
and add the following code in WebApiConfig.cs file from App_Start folder:
并从App_Start文件夹中的WebApiConfig.cs文件中添加以下代码:
var enableCorsAttribute = new EnableCorsAttribute("*",
"Origin, Content-Type, Accept",
"GET, PUT, POST, DELETE, OPTIONS");
config.EnableCors(enableCorsAttribute);