CodeIgniter Forums
403 error in AJAX to Controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: 403 error in AJAX to Controller (/showthread.php?tid=74703)



403 error in AJAX to Controller - anonymous5421 - 10-27-2019

Hello. I am trying to send an string from "View" to the "Controller" with AJAX, but give Error 403. (CodeIgniter 4)


RE: 403 error in AJAX to Controller - ciadmin - 10-27-2019

You need to provide a lot more information than just this, before anyone can help you.
The only thing someone could point out is that a 403 is "access forbidden", if that helps.


RE: 403 error in AJAX to Controller - mintwint - 10-27-2019

check csrf token


RE: 403 error in AJAX to Controller - ecampait - 10-27-2019

Hi,

as said @mintwint you must have the CSRF enabled. In your code ajax replaces the method POST by GET and if it works it is the CSRF which has the cause. In this case, add the CSRF name and hash in the data string.


RE: 403 error in AJAX to Controller - anonymous5421 - 10-27-2019

(10-27-2019, 07:22 AM)mintwint Wrote: check csrf token
How should I do this?
in the $globals in config/Filters.php, csrf is commented:
public $globals = [
'before' => [
//'honeypot'
// 'csrf',
],
'after' => [
'toolbar',
//'honeypot'
],
];


RE: 403 error in AJAX to Controller - anonymous5421 - 10-27-2019

(10-27-2019, 07:12 AM)ciadmin Wrote: You need to provide a lot more information than just this, before anyone can help you.
The only thing someone could point out is that a 403 is "access forbidden", if that helps.
View->textbox.php
Code:
<div style="text-align:center;">
    <input id="txt_short" type="text"/>
    <input id="btn_short" type="submit" value="Go" />
    <p id="show"></p>
</div>
<script>
                $(document).ready(function(){
                $("#btn_short").click(function(){
                var l =$("#txt_short").val();
                $.ajax({type:"POST",url:"../../App/Controllers/Links.php",data:"?l="+l,done:function(msg){
                    $("#show").html(data);
                        }});
                     });
                 });
</script>

Controller->Links.php
PHP Code:
class Links extends Controller
{
        public function index()
        {
                helper('html');
                helper('form');
                $p$_POST["li"];
        }




RE: 403 error in AJAX to Controller - anonymous5421 - 10-27-2019

(10-27-2019, 07:55 AM)ecampait Wrote: Hi,

as said @mintwint you must have the CSRF enabled. In your code ajax replaces the method POST by GET and if it works it is the CSRF which has the cause. In this case, add the CSRF name and hash in the data string.

I enabled CSRF and changed textbox.php View:
Code:
<div style="text-align:center;">
    <input id="txt_short" type="text"/>
    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>" />
    <input id="btn_short" type="submit" value="Go" />
    <p id="show"></p>
</div>
<script>
                $(document).ready(function(){
                $("#btn_short").click(function(){
                var l =$("#txt_short").val();
                var myObj = {csrf_test_name:"<?= csrf_hash() ?>", li:l };
                $.ajax({datatype:'json', type:'method', contentType: 'application/json; charset=utf-8', url='../../App/Controllers/links/index',data: stringify(myOBJ),done:function(msg){
                    $("#show").html(data);
                        }});
                     });
                 });
</script>

and this code is my Controller:
PHP Code:
class Links extends Controller
{
        public function index()
        {
                $security = \Config\Services::security();
                helper('security');
                helper('html');
                helper('form');
                helper('url');
                helper('text');
                return view('textbox');
        }




RE: 403 error in AJAX to Controller - InsiteFX - 10-28-2019

Your getting an error because your Ajax base url is wrong.

Add to your header on top at the bottom before the closing </head> tag

Code:
<!-- Pass base_url() and site_url() to JavaScript -->
<script>
    var baseUrl = "<?= base_url();?>";
    var siteUrl = "<?= site_url();?>";
</script>

Then in your ajax method add this.

Code:
// Ajax url:
url: baseUrl + "links/index",

Try that.