Welcome Guest, Not a member yet? Register   Sign In
403 error in AJAX to Controller
#1

(This post was last modified: 10-27-2019, 05:37 AM by anonymous5421.)

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

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.
Reply
#3

check csrf token
Reply
#4

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.
Reply
#5

(This post was last modified: 10-27-2019, 08:27 AM by anonymous5421.)

(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'
],
];
Reply
#6

(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"];
        }

Reply
#7

(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');
        }



Attached Files Thumbnail(s)
   
Reply
#8

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB