Controller ajax request |
Hi guys im new to codeigniter im still figuring out things here. So i have a problem about the ajax request it seem that the controller is not accepting the ajax request. I`m codeigniter 4.0 by the way. I have search a lot of material in the internet and youtube but still nothing works.
So heres the code in the view folder named layout.php Code: <!DOCTYPE html> PHP Code: <?php Code: $routes->get('/', 'Home::index'); Now what I`m doing here is just trying to test if ajax works and return something. If I uncomment the form tag in the layout file the request works fine but I need the request given by ajax not in the form tag so I wont use it but if I use the ajax it wont respond anything and there is no error like in ci4 and the jquery. But the weird thing is that when I use post man and send ajax request it works perfectly fine. Can someone point out what I`m missing here?
There is bugs in your JavaScript. code. You send tokenName and tokenVal but the variables doesn't exists. They are in comments. Look in the browser console, you will have an error message.
In Config/Filters.php enabled csrf?
If enabled - must send csrf token for POST request
The idea behind MVC is that you separate the various components of your application so they can operate on a "need to know" basis.
Your models should be concerned with data, like say, a "Customer" record. A database should not give two shits about the type of <div> the customer's first name is going to be wrapped in. (Side note: Seeing HTML in a DB table is one of those things that is just wrong.) Controllers do the dispatch to the models, and extract relevant data from the models, and forward it out to your Views. A View takes some set of expected data and transforms it into a something that would make sense in the context of the view. If you're sending a confirmation email, you might forward to an "Email" view with data like "email address" and "message body". 'm in the middle of a web development learning project. I'm working on implementing the MVC pattern using PHP+Smarty templates for the "framework." So far, I have implemented the basics of the app (base classes for models, views, and controllers as well as test cases for each and example db queries etc.), and am planning to integrate JavaScript (+jQuery) and AJAX to submit things like comments and feedback so I don't have to redirect the user anywhere. Thus far, MVC has been a dream. Aside from learning the basics, I never really jumped on the Rails bandwagon but I wholly appreciate the clean and separated approach to development (my education is in C programming). My question is where/how should I integrate ajax support into my framework? My file structure looks like: app controllers helpers models views config include basecontroller.php basemodel.php baseview.php
I found issue in your ajax. I have fixed it. I have corrected url of ajax and remove data.
$( document ).ready(function() { $( ".ri-remote-control-line" ).click(function() { var lockID = this.id; //I am developerbook chatrandom getting value here $.ajax({ headers: { 'X-CSRF-TOKEN': $('meta omegle [name="csrf-token"]').attr('content') }, url: '/unlock/'+lockID, type: "GET", dataType: "JSON", processData: false, contentType: false, success: function (data, status) { console.log(data); }, error: function (xhr, desc, err) { console.log(xhr); } }); }); });
Makitweb - How to Send AJAX request with CSRF token in CodeIgniter 4
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |