Sava localstorage data

Hi,
I make a cart. Cart implemented on the client side within the means js and save in localstorage. Tell me how to save data from localstorage in DB. I tried passing from using ajax but I always have errors.
this is my function js. I want to transfer data to cantroller of this view

function checkout(){
let order = [];

for (var i = 0; i < count; i++) {
	let key   = localStorage.key(i);
	order[i] += JSON.stringify(localStorage.getItem(key));
}

var rout = "<?php Router::url(['controller' => 'cart', 'action'=>'addOrder']); ?>";

$.ajax({
	type: 'post',
	url: rout,
	data: order,
	dataType: 'json',
	success: function(){
		alert('ОП! ОП! ОП!');
	},
	error: function(e){
		alert('ERROR! : ' + e.status + ' : ' + e.statusText);
	}
});

}

Is your function.js generated by Cake, or a static file? If the latter, then your <?php Router::url(...) ?> is just going to be seen as-is as a string in your JS code by the browser. (That is, the JS will think that the URL to post to is literally “<?php Router::url(...) ?>”.) And if the former, your problem is going to start with the fact that you’re not actually outputting the result of that Router::url call; <?= Router::url(...) ?> will fix that.

Beyond that, there may well be other errors. When you don’t give any details about them, but just say “I always have errors”, that’s not helpful.

This is a statistical file. Can you tell me how to save data from localstorage in the database? This is my first project. I’ve been sitting on this for 2 days.

At least an algorithm and what to read. Because I can’t find anything at all.

If it’s static, you need to have a static string in there instead of Cake code. Replace it with whatever the actual URL is supposed to be.

function checkout(){
let order;
let obj;

for (var i = 0; i < count; i++) {
	let key   = localStorage.key(i);
	obj += localStorage.getItem(key);
	order = JSON.stringify(obj);
}

$.ajax({
	type: 'post',
	url: 'cart/addOrder',
	data: order,
	dataType: 'json',
	success: function(){
		alert('ОП! ОП! ОП!');
	},
	error: function(e){
		alert('ERROR! : ' + e.status + ' : ' + e.statusText);
	}
});

}

did not help. Error same : POST http://thc:81/cart/addOrder 403 (Forbidden)

cart - controller
addOrder - the method in which the preservation should take place.

Is http://thc:81/cart/addOrder the correct URL that it’s supposed to be accessing? If so, you need to determine whether the 403 error is coming from your web server (check Apache logs) or Cake. If that’s not the right URL, then you’re going to need to use the entire URL in the Ajax call instead of just cart/addOrder.

2019-12-30 14:27:25 Error: [Cake\Routing\Exception\MissingControllerException] Controller class Js could not be found. (C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\ControllerFactory.php:104)
Exception Attributes: array (
‘class’ => ‘Js’,
‘plugin’ => false,
‘prefix’ => false,
‘_ext’ => false,
)
#0 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\ControllerFactory.php(39): Cake\Http\ControllerFactory->missingController(Object(Cake\Http\ServerRequest))
#1 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\ActionDispatcher.php(91): Cake\Http\ControllerFactory->create(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#2 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\BaseApplication.php(234): Cake\Http\ActionDispatcher->dispatch(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#3 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Http\BaseApplication->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#4 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#5 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Middleware\CsrfProtectionMiddleware.php(123): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#6 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Http\Middleware\CsrfProtectionMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#7 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(51): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#8 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Routing\Middleware\RoutingMiddleware.php(168): Cake\Http\Runner->run(Object(Cake\Http\MiddlewareQueue), Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#9 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Routing\Middleware\RoutingMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#10 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Routing\Middleware\AssetMiddleware.php(97): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#11 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Routing\Middleware\AssetMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#12 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Error\Middleware\ErrorHandlerMiddleware.php(96): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#13 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Error\Middleware\ErrorHandlerMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#14 C:\OSPanel\domains\THC\vendor\cakephp\debug_kit\src\Middleware\DebugKitMiddleware.php(53): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#15 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): DebugKit\Middleware\DebugKitMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#16 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(51): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#17 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Server.php(97): Cake\Http\Runner->run(Object(Cake\Http\MiddlewareQueue), Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#18 C:\OSPanel\domains\THC\webroot\index.php(40): Cake\Http\Server->run()
#19 {main}
Request URL: /js/bootstrap/popper.js.map

2019-12-30 14:27:27 Error: [Cake\Routing\Exception\MissingControllerException] Controller class Js could not be found. (C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\ControllerFactory.php:104)
Exception Attributes: array (
‘class’ => ‘Js’,
‘plugin’ => false,
‘prefix’ => false,
‘_ext’ => false,
)
#0 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\ControllerFactory.php(39): Cake\Http\ControllerFactory->missingController(Object(Cake\Http\ServerRequest))
#1 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\ActionDispatcher.php(91): Cake\Http\ControllerFactory->create(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#2 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\BaseApplication.php(234): Cake\Http\ActionDispatcher->dispatch(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#3 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Http\BaseApplication->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#4 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#5 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Middleware\CsrfProtectionMiddleware.php(123): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#6 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Http\Middleware\CsrfProtectionMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#7 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(51): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#8 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Routing\Middleware\RoutingMiddleware.php(168): Cake\Http\Runner->run(Object(Cake\Http\MiddlewareQueue), Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#9 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Routing\Middleware\RoutingMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#10 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Routing\Middleware\AssetMiddleware.php(97): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#11 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Routing\Middleware\AssetMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#12 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Error\Middleware\ErrorHandlerMiddleware.php(96): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#13 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Error\Middleware\ErrorHandlerMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#14 C:\OSPanel\domains\THC\vendor\cakephp\debug_kit\src\Middleware\DebugKitMiddleware.php(53): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#15 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): DebugKit\Middleware\DebugKitMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#16 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(51): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#17 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Server.php(97): Cake\Http\Runner->run(Object(Cake\Http\MiddlewareQueue), Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#18 C:\OSPanel\domains\THC\webroot\index.php(40): Cake\Http\Server->run()
#19 {main}
Request URL: /js/bootstrap/bootstrap.min.js.map

2019-12-30 14:27:33 Error: [Cake\Http\Exception\InvalidCsrfTokenException] CSRF token mismatch. (C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Middleware\CsrfProtectionMiddleware.php:231)
#0 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Middleware\CsrfProtectionMiddleware.php(151): Cake\Http\Middleware\CsrfProtectionMiddleware->_validateToken(Object(Cake\Http\ServerRequest))
#1 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Middleware\CsrfProtectionMiddleware.php(121): Cake\Http\Middleware\CsrfProtectionMiddleware->_validateAndUnsetTokenField(Object(Cake\Http\ServerRequest))
#2 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Http\Middleware\CsrfProtectionMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#3 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(51): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#4 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Routing\Middleware\RoutingMiddleware.php(168): Cake\Http\Runner->run(Object(Cake\Http\MiddlewareQueue), Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#5 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Routing\Middleware\RoutingMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#6 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Routing\Middleware\AssetMiddleware.php(88): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#7 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Routing\Middleware\AssetMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#8 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Error\Middleware\ErrorHandlerMiddleware.php(96): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#9 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Error\Middleware\ErrorHandlerMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#10 C:\OSPanel\domains\THC\vendor\cakephp\debug_kit\src\Middleware\DebugKitMiddleware.php(53): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#11 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(65): DebugKit\Middleware\DebugKitMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#12 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Runner.php(51): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#13 C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Http\Server.php(97): Cake\Http\Runner->run(Object(Cake\Http\MiddlewareQueue), Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#14 C:\OSPanel\domains\THC\webroot\index.php(40): Cake\Http\Server->run()
#15 {main}
Request URL: /cart/addOrder
Referer URL: http://thc:81/cart

these are logs cake

[27-Dec-2019 20:09:36 UTC] PHP Fatal error: Maximum execution time of 180 seconds exceeded in C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Mailer\TransportFactory.php on line 23
[27-Dec-2019 20:09:36 UTC] PHP Fatal error: Maximum execution time of 180 seconds exceeded in C:\OSPanel\domains\THC\vendor\cakephp\cakephp\src\Mailer\TransportFactory.php on line 23

and these are logs Apache.

I couldn’t find an answer here, maybe you can understand something here.

I also tried to register it in the controller

public function addOrder(){
$this->autoRender = false;
$this->request->onlyAllow(‘ajax’);
}

The “CSRF token mismatch” error indicates that you have the CSRF protection enabled, but are not sending the required header. There’s loads of solutions for this one out there.

I get a token like this

$token = json_encode($this->request->param(’_csrfToken’));

and pass it to the script

$.ajax({
	type: 'post',
	url: targeturl,
	data: order,
	dataType: 'json',
    beforeSend: function(xhr) 
    {
        xhr.setRequestHeader('X-CSRF-Token', csrfToken);
    },

and the exact same mistake “CSRF token mismatch.”

How are you passing it to the script? Have you confirmed that csrfToken has the correct value when you make this call? Why are you json_encodeing it?

I removed json_encode and it worked. but for some reason the answer is returned not through success, but through error. Why?

  1. responseText: “

    undefined{“id”:2,“category_id”:2,“name”:“apple”,“price”:10,“image”:“img\product_img\apple.jpg”,“Categories”:{“id”:2,“name”:“fruits”}}{“id”:2,“category_id”:2,“name”:“apple”,“price”:10,“image”:“img\product_img\apple.jpg”,“Categories”:{“id”:2,“name”:“fruits”}}{“id”:12,“category_id”:2,“name”:“Orange”,“price”:14,“image”:“img\product_img\orange.jpg”,“Categories”:{“id”:2,“name”:“fruits”}}{“id”:2,“category_id”:2,“name”:“apple”,“price”:10,“image”:“img\product_img\apple.jpg”,“Categories”:{“id”:2,“name”:“fruits”}}{“id”:3,“category_id”:1,“name”:“potatoes”,“price”:7,“image”:“img\product_img\potatoes.jpg”,“Categories”:{“id”:1,“name”:“vegetables”}}!!!

    ↵”
  2. status: 200
  3. statusText: “OK”

Nothing here indicates an error. What haven’t you told us?

One possible problem that’s been in both the javascript blocks you’ve shown:

$.ajax({
	type: 'post',
	url: rout,
	data: order,
	dataType: 'json',
	success: function(){
		alert('ОП! ОП! ОП!');
	},
	error: function(e){
		alert('ERROR! : ' + e.status + ' : ' + e.statusText);
	}
});

I believe success: function() { } should get the even parameter:

success: function(e) { }

perhaps you’ve already fixed that?

Yes, I fixed this, but this not help.

Ohh! If I know … But everything works.