0
votes

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:

  $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.

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
Method not allowed (Status code 405) means you don't have any OPTIONS method defined on your endpoint. It's mandatory for CORS requests. See CORS Can you give any details about your endpoint? (Language, Frameworks, etc.) - KRONWALLED
@Kronwalled just added the call to the question body. Thx for helping me! - Laziale

2 Answers

0
votes

You can add a WebInvoke for Method OPTIONS to your ServiceContract like this:

[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
}
0
votes

Do the following:

 Install-Package Microsoft.AspNet.WebApi.Cors

then go to Web.config and remove these lines (if you have them)

<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:

var enableCorsAttribute = new EnableCorsAttribute("*",
                          "Origin, Content-Type, Accept",
                          "GET, PUT, POST, DELETE, OPTIONS");
config.EnableCors(enableCorsAttribute);