# SOLVED: Cannot receive POST-request from JS

**URL:** <https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344>\
**Category:** Need Help\
**Created:** [July 4, 2019, 3:08pm UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344 "2019-07-04T15:08:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marlin](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@Marlin](https://discourse.cakephp.org/u/Marlin)\
**Post date:** [July 4, 2019, 3:08pm UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/1 "2019-07-04T15:08:13Z")

</div>

Hey,  
i’m currently working on a project, where i have to pass data from a JS client to a CakePHP server.  
I’m trying to do this via POST request, but the request isn’t even recevied by the controller.  
For this i’m running XAMPP localy on my computer.

In JS: (used absolute path to be absolutly sure, the right function gets called, gonna change that in the end of course)

> $.post(‘[http://localhost:8008/ersatzteiltool/transferData](http://localhost:8008/ersatzteiltool/transferData)’, cmd.payload, function(response) {  
> alert(‘succsess!’)  
> }, ‘json’);

I’m simply trying to pass some formatted JSON-Data (cmd.payload) to CakePHPs remoterender Controller (using an alternative route see below).

For routing in routes.php

> $routes-\>connect(‘/transferData’, [‘controller’ =\> ‘remoterender’, ‘action’ =\> ‘orderSubmit’]);

and in RemoteRenderController.php

> public function orderSubmit() {  
> $this-\>layout = null;  
> $jsonData = $this-\>request-\>input(‘json\_decode’);  
> //or  
> //$data = $this-\>request-\>getData();  
> Log::write(‘debug’, 'orderSubmit reached, data: ’ . $jsonData);  
> }

This way the orderSubmit()-Function isn’t even called. When i change my POST-Request to a GET-Request in JS, the Controller recieves the request, but there is only an empty array passed.

I tried different things already:

- sending not JSON-formatted Data or simple data like

> ‘data[User][id]’:‘1234’, ‘data[User][name]’:‘John’

(still an empty array arriving with GET, nothing with POST)

- using AJAX POST/GET Request (same result as now)
- tried without using an alternativ route

Anybody got any ideas? Am i just missing the obvious? First time working with Php.

Ideas i had, but couldn’t get working:

- disable cors (maybe its blocking POST-Requests from AJAX?)

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [July 4, 2019, 8:05pm UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/2 "2019-07-04T20:05:46Z")

</div>

What scope is your route in?

---

<div class="post-metadata">

**Author:** ![Marlin](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@Marlin](https://discourse.cakephp.org/u/Marlin)\
**Post date:** [July 8, 2019, 7:06am UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/3 "2019-07-08T07:06:18Z")

</div>

in Controller/RemoteRenderController.php

class RemoteRenderController {  
orderSubmit() {  
}  
}

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [July 8, 2019, 7:21am UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/4 "2019-07-08T07:21:59Z")

</div>

You need to define your routes in `config/routes.php`…  
here is mine for reference:

> <https://gitlab.com/FinlayDaG33k/Website/blob/master/www/config/routes.php>

---

<div class="post-metadata">

**Author:** ![Marlin](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@Marlin](https://discourse.cakephp.org/u/Marlin)\
**Post date:** [July 8, 2019, 7:53am UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/5 "2019-07-08T07:53:09Z")

</div>

I did that, but maybe if have to define more routes?

As it is i wrote in routes.php:

$routes-\>connect(’/transferData’, [‘controller’ =\> ‘remoterender’, ‘action’ =\> ‘orderSubmit’]);

The thing that still confuses me, is that if i send a get request the function is called… (same as when i just call the URL in a browser)

EDIT:  
Chromes developer console tells a little bit more:  
Failed to load resource: the server responded with a status of 403 (Forbidden).

EDITEDIT:  
I think it’s a problem with the auth component, i guess i got no rights, because my js client isn’t logged in…  
–\> added to the beforeFilter function: $this-\>Auth-\>allow([‘orderSubmit’]);  
but its still not working. Nevertheless forbidden access.

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [July 8, 2019, 8:34am UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/6 "2019-07-08T08:34:45Z")

</div>

Considering it’s an xhr request, try setting the csrf token in the headers? you’ll need to change your ajax call to:

```auto
$.ajax({
  url: '', // add your url here
  headers: {
    'X-CSRF-Token': <?= json_encode($this->request->getParam('_csrfToken')); ?>
  }
  data: '' // your data here
}).done(function(response) {
  // Do stuff here
});

```

---

<div class="post-metadata">

**Author:** ![Marlin](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@Marlin](https://discourse.cakephp.org/u/Marlin)\
**Post date:** [July 8, 2019, 8:37am UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/7 "2019-07-08T08:37:48Z")

</div>

Thanks for your fast replies!  
I tried:  
beforeSend: function (xhr) {  
xhr.setRequestHeader(‘X-CSRF-Token’, $(’[name="\_csrfToken"]’).val());  
},  
as parameter already, but gonna try yours as well.

–  
I am confused, why is there a PHP snippet in the header? Shoud i pass it as a string? Or do you define the JS code in one of the “view”.php files?

–  
Passed it as a string and at least i recevied it finally. Your great man, big thanks to you!

–  
Well i guess it’s only passing because it didnt have the type=“post”… with that it’s still not working…

with get i am receving an array, but i’m not sure what the actual content is. logging only says array.

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [July 8, 2019, 9:59am UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/8 "2019-07-08T09:59:29Z")

</div>

what does your current ajax call look like?

---

<div class="post-metadata">

**Author:** ![Marlin](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@Marlin](https://discourse.cakephp.org/u/Marlin)\
**Post date:** [July 8, 2019, 10:16am UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/9 "2019-07-08T10:16:52Z")

</div>

$.ajax({  
url: ‘[http://localhost:8008/ersatzteiltool/remoterender/orderSubmit](http://localhost:8008/ersatzteiltool/remoterender/orderSubmit)’,  
headers: {  
‘X-CSRF-Token’: “\<?= json\_encode($this-\>request-\>getParam('\_csrfToken')); ?\>”  
},  
contentType: “application/json; charset=utf-8”,  
dataType: “json”,  
type: “post”,  
data: JSON.stringify(cmd),  
}).done(function(response) {  
console.log(“POST got THROUGH :)”);  
});

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [July 8, 2019, 11:15am UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/10 "2019-07-08T11:15:45Z")

</div>

Hmm… may I note that your route says `/transferData` while your ajax call is requesting `orderSubmit`…  
Please make sure you are using the right url 😛  
Or even better, use CakePHP’s url builder (along with the reverse routing capability)!

```auto
$.ajax({
  url: `<?= h($this->Url->build(['controller' => 'RemoteRender', 'action' => 'orderSubmit'])); ?>`,
  // ... rest of your ajax stuff
})

```

[https://book.cakephp.org/3.0/en/views/helpers/url.html](https://book.cakephp.org/3.0/en/views/helpers/url.html)

---

<div class="post-metadata">

**Author:** ![Marlin](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@Marlin](https://discourse.cakephp.org/u/Marlin)\
**Post date:** [July 8, 2019, 12:26pm UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/11 "2019-07-08T12:26:43Z")

</div>

Interesting Idea! I’ll try it.

Yeah sorry, in the meantime i changed the route in routes.php to:  
$routes-\>connect(’/remoteRender’, [‘controller’ =\> ‘remoterender’, ‘action’ =\> ‘index’, ‘orderSubmit’, ‘userRedirect’]);

just trying out different things…

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [July 8, 2019, 7:10pm UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/12 "2019-07-08T19:10:59Z")

</div>

Just make sure that either:

1. your array in the url builder matches the one in your route
2. your url matches your scope

Example given:

# case 1.:

```php
// config/routes.php
Router::scope('/', function (RouteBuilder $routes) {
  $routes->connect('/myRoute', ['controller' => 'MyController', 'action' => 'index']);
});

```

```auto
// script.js
$.ajax({
  url: `<?= h($this->Url->build(['controller' => 'MyController', 'action' => 'index'])); ?>`,
  // ... rest of the ajax stuff
})

```

# case 2.:

```php
// config/routes.php
Router::scope('/', function (RouteBuilder $routes) {
  $routes->connect('/myRoute', ['controller' => 'MyController', 'action' => 'index']);
});

```

```auto
// script.js
$.ajax({
  url: `/myRoute`,
  // ... rest of the ajax stuff
})

```

## Recommendation:

Use option 1 as this allows you to change the url without changing the route itself.  
So let’s say you want to change the `/myRoute` part to `/asdf`, you can do so without having to change your views (and thus prevent potential breakage of your app).  
The url builder will match the array you’ve given it and spit out the right url!

---

<div class="post-metadata">

**Author:** ![njuejohn](https://avatars.discourse-cdn.com/v4/letter/n/ba9def/32.png) [@njuejohn](https://discourse.cakephp.org/u/njuejohn)\
**Post date:** [July 11, 2019, 3:59am UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/13 "2019-07-11T03:59:08Z")

</div>

Try axios js. Remember to add csrfToken to the header. Example.

axios.defaults.headers.common[‘X-CSRF-Token’] = “\<?php echo $this-\>request-\>getParam('\_csrfToken'); ?\>”;  
axios.defaults.headers.common[‘Cache-Control’] = “no-store, private, no-cache, must-revalidate”;  
axios.defaults.headers.common[‘Expires’] = 0;

axios.post(‘your controller/service method/optional pass data’, {data:{key:value data to the server}})  
.then(function (response) {//process response

})  
.catch(function (error) {//Catch that nasty error from the server

});

---

<div class="post-metadata">

**Author:** ![Marlin](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@Marlin](https://discourse.cakephp.org/u/Marlin)\
**Post date:** [July 11, 2019, 11:28am UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/14 "2019-07-11T11:28:33Z")

</div>

Thanks for your help!  
Tried FinlayDaG33ks solution and njuejohns with axios, now if i log the error object from a axios request i get:  
Error: “Request failed with status code 403”  
exports [https://unpkg.com/axios/dist/axios.min.js:8](https://unpkg.com/axios/dist/axios.min.js:8)  
exports [https://unpkg.com/axios/dist/axios.min.js:8](https://unpkg.com/axios/dist/axios.min.js:8)  
onreadystatechange [https://unpkg.com/axios/dist/axios.min.js:8](https://unpkg.com/axios/dist/axios.min.js:8)  
logAjaxRequest [http://localhost:8008/debug\_kit/js/toolbar.js?1561463013:74](http://localhost:8008/debug_kit/js/toolbar.js?1561463013:74)

I managed to decode the recevied data from the get-request, problem is, i initally wanted to send some encoded image files, as i know i can’t do that with a get-request because of the limited url size?

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [July 12, 2019, 11:43am UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/15 "2019-07-12T11:43:12Z")

</div>

Why on earth would you want to upload an image using a GET request 😛  
We have `PUT` for that stuff 😛  
Also, you might want to send the data chunked in order to not hit a POST-size limit randomly…

---

<div class="post-metadata">

**Author:** ![Marlin](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@Marlin](https://discourse.cakephp.org/u/Marlin)\
**Post date:** [July 12, 2019, 12:21pm UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/16 "2019-07-12T12:21:55Z")

</div>

Yeah as i said, i want to send my images with a POST (PUT would be fine aswell) request, but i really cant get it working with CakePHP.

I don’t want to send data with a get request, thats litterly not what the request is made for,b ut at the moment its the only way i can receive data at all…

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [July 12, 2019, 3:04pm UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/17 "2019-07-12T15:04:16Z")

</div>

What do your logs have to say about the cause of the 403 error? Those don’t typically happen without leaving some details.

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [July 12, 2019, 3:40pm UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/18 "2019-07-12T15:40:22Z")

</div>

I think that if all this stuff doesn’t work, you have something else causing the 403…  
What is the output you’re getting?  
Like, when I get an error, I get a huge red screen in my face telling me exactly what went wrong…

---

<div class="post-metadata">

**Author:** ![kailashdesk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kailashdesk/32/1493_2.png) [@kailashdesk](https://discourse.cakephp.org/u/kailashdesk)\
**Post date:** [July 15, 2019, 5:19am UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/19 "2019-07-15T05:19:11Z")

</div>

I think you need to set $this-\>autoRender to false and $this-\>layout to false.

---

<div class="post-metadata">

**Author:** ![Marlin](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@Marlin](https://discourse.cakephp.org/u/Marlin)\
**Post date:** [July 15, 2019, 8:04am UTC](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344/20 "2019-07-15T08:04:09Z")

</div>

Well, probably i should have started with looking into the error.log files…  
it says _ **CSRF token mismatch.** _  
In this thread there were some suggestions how to send a valid CSRF header.

I tried so far using ajax:  
`headers: { 'X-CSRF-Token': <?= json_encode($this->request->getParam('_csrfToken')); ?> }`  
and axios:

```
axios.defaults.headers.common[‘X-CSRF-Token’] = “<?php echo $this->request->getParam('_csrfToken'); ?>”;

```

the log says with both ways CSRF token mismatch.

I added in the Application.php a new CsrfProtectionMiddleware for the middlewareQueue, no changes.

[Next page](https://discourse.cakephp.org/t/solved-cannot-receive-post-request-from-js/6344.md?page=2)
