0

This question is sort of related to this question I posted.

Allow my API to be access via AJAX

I manage to fix the CORS error, I think by adding following the instructions on http://enable-cors.org/server_apache.html. The issue doesn't appear now.

But this time, I am getting a 405 error, to which I am not sure why so.

Client

var appOrigin = 'http://event.chart.local/api/vendor/events.json';
app.factory('chartListLoadService',['$http',function($http){
  return {
      fetchList: function(city){
          var data      = {city:city};
          $http({
              method:'post',
              url:appOrigin,
              data:data
          }).success(function(data){
              alert(data);
          });
      },
      testFunction:function(){
          alert('testing');
      }
  };
}]);

Server

public function post_events()
{
    $city = Input::post('city','all');
    $c = Model_chart::format_chart($city);
    return $this->response($c);
}

Header Info

Remote Address:127.0.0.1:80
Request URL:http://event.chart.local/api/vendor/events.json
Request Method:OPTIONS
Status Code:405 Method Not Allowed
Request Headersview parsed
OPTIONS /api/vendor/events.json HTTP/1.1
Host: event.chart.local
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://app.event.chart
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36
Access-Control-Request-Headers: accept, content-type
Accept: */*
Referer: http://app.event.chart/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Response Headersview parsed
HTTP/1.1 405 Method Not Allowed
Date: Fri, 26 Sep 2014 02:48:33 GMT
Server: Apache/2.4.9 (Win32) OpenSSL/1.0.1g PHP/5.5.11
X-Powered-By: PHP/5.5.11
Access-Control-Allow-Origin: http://app.event.chart
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: Content-Type
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html
Community
  • 1
  • 1
Mr A
  • 1,345
  • 4
  • 22
  • 51
  • does your server allow `OPTIONS` requests? Try adding `Access-Control-Allow-Methods: POST, GET, OPTIONS` to the responses. – akonsu Sep 26 '14 at 03:02
  • Yes. As seen on the header information above. @akonsu – Mr A Sep 26 '14 at 03:05
  • Are you sure it is `events.json`. How can you post to a json file? Did you mean `get` or is the api something else? – PSL Sep 26 '14 at 03:24
  • @psl it's a fuel thing. It means that the response will be encoded in json, that's what the .json is for in the url. – Mr A Sep 26 '14 at 03:43

1 Answers1

0

From the FuelPHP side:

Assuming you use Controller_Rest to create this functionality:

That returns a 405 status if it can't match the request to any method in the controller.

Again, assuming with a link like http://event.chart.local/api/vendor/events.json, your controller is Controller_Api_Vendor (or a namespace variant), and the method is "events"?

In that case, it will look for either options_events(), or action_events(). If one of these exist and is callable, the 405 is not generated by Fuel.

WanWizard
  • 2,574
  • 15
  • 13
  • The link works on normal curl request. There it is using Controller_Rest and it is on the right method as indicated in the post_event function name. – Mr A Sep 26 '14 at 07:48
  • Then I can only assume your webserver isn't configured to accept these requests – WanWizard Sep 26 '14 at 16:00