0

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
  • 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.) Commented Jun 9, 2016 at 14:27
  • @Kronwalled just added the call to the question body. Thx for helping me! Commented Jun 9, 2016 at 14:59

2 Answers 2

0

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Kronwalled. Once I'll add that, how can I utilize it for my call? Or that's the only thing I need to do?
It should be automatically invoked when you fire the GET request
Getting this error after the changes i.gyazo.com/83267cb7a48c2a1167c015f83b160079.png
Could you update the post how the whole service looks right now?
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.