bcosca / fatfree

A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sending Fetch API Post to Controller - No Response

AndrewMarkUK opened this issue · comments

I have be trying to send a Fetch API Post to a controller. Though the below is quite simple, for the purpose of this help question, I have tried many ways but quite simply, the controller shows no response. And so...the fetch request....

fetch("/shop/additem", {
headers: {
'X-Requested-With': 'XMLHttpRequest',
//"Content-Type": "application/x-www-form-urlencoded",
//"Accept":"application/json"
},
body: 'ID=1&Name=Test',
method: "post"
});

In my Router I have...

$f3->route('POST /shop/additem [ajax]', 'ShopController->additem');

In the ShopController, I have...

class ShopController{

public function addItem($f3) {
print_r($_POST);
}

}

As you see, nothing above is rocket science.

However when examining inside Chrome Dev Tools firstly under Headers the Form Data is populated with the values submitted, so the values are being submitted BUT, when looking in Preview and Response Tabs (again under Chrome Dev Tools), its empty, nothing. The print_r function in the ShopController is not showing anything.

Furthermore however, when I changed fetch to post directly to a url outside of the framework, in my case I simply changed it to 'test.php', it worked and the print_r showed the values posted in the page. In the test.php page it only contained...

print_r($_POST);

Ultimately I am left to ask why it will post successfully to a direct url but it will not work within FF posting to a route?

Any helps, hints and tips gratefully received.

PS: you will see in fetch there are some other headers which are commented out. I tried with these and made no difference.

not sure if it makes a difference but your method in the route call has a wrong case ->
ShopController->additem vs public function addItem ..notice the i