405 Method not allowed

OPTIONS http://testing.iwonwebtech.com/mba/api/webservices/login 405 (Method Not Allowed)
e @ polyfills.js:3
t.scheduleTask @ polyfills.js:3
e.scheduleMacroTask @ polyfills.js:3
(anonymous) @ polyfills.js:3
send @ VM11114:3
(anonymous) @ xhr_backend.js:117
Observable.subscribe @ Observable.js:56
Webservice.LoginUser @ webservice.ts:47
LoginPage.loginForm @ login.ts:54
View_LoginPage0.handleEvent_43 @ /AppModule/LoginPage/component.ngfactory.js:581
(anonymous) @ view.js:408
(anonymous) @ dom_renderer.js:276
t.invokeTask @ polyfills.js:3
onInvokeTask @ ng_zone.js:227
t.invokeTask @ polyfills.js:3
e.runTask @ polyfills.js:3
invoke @ polyfills.js:3
(index):1 XMLHttpRequest cannot load http://testing.iwonwebtech.com/mba/api/webservices/login. Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:8100’ is therefore not allowed access. The response had HTTP status code 405.

NOTE : i am USING CODE IGNITER rest library for the same.

  1. Make the server respond to OPTIONS requests
  2. Add the mentioned header to it
  3. Tadaaa

You should ONLY have the Access-Control-Allow-Origin: * set when you’re developing using your browser, especially if the API is not only used for the app but also some other web based application! This is not required for the phone, as the files are served from the file:// URI and cross domain policies do not apply. Either you set the ACAO on the server, or setup a proxy in your ionic.config.json file during development

- By me from here

When it comes to the OPTIONS request, do as @Sujan12 says and make the server respond 200 OK to all OPTIONS requests. If you have any custom headers and what not, make sure to set the Access-Control-Allow-Headers header in the response to the preflight.

If this is your API then add this on Top in your PHP file if you are testing in Browser but only for testing. Delete this lines if you release your Application:

For POST Methods:

/*------------------------------------------------------*/
if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');    // cache for 1 day
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
 
	if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
        header("Access-Control-Allow-Methods: POST, POST, OPTIONS");         
 
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
        header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
 
    exit(0);
}
/* -----------------------------------------------------*/

For GET Methodes:

/*------------------------------------------------------*/
if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');    // cache for 1 day
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
 
	if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
        header("Access-Control-Allow-Methods: GET, GET, OPTIONS");         
 
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
        header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
 
    exit(0);
}
/* -----------------------------------------------------*/

And be sure to Allow Origin in your Application as well in your config.xml

<access origin="*" subdomains="true" />
<allow-navigation href="*" />

I have tried

But still it’s not work

What have you tried? Is the server now answering the OPTIONS request with somethig else than 405? Did you add the header because the problem persisted even with a working OPTIONS request?

Thx for Help Issue Solved :slight_smile:

Cool! :slight_smile: then mark it as solution pls :slight_smile:

2 Likes

How did you solved this issue?Can you please help?