Welcome Guest, Not a member yet? Register   Sign In
403 error in AJAX to Controller
#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


Messages In This Thread
RE: 403 error in AJAX to Controller - by ciadmin - 10-27-2019, 07:12 AM
RE: 403 error in AJAX to Controller - by mintwint - 10-27-2019, 07:22 AM
RE: 403 error in AJAX to Controller - by ecampait - 10-27-2019, 07:55 AM
RE: 403 error in AJAX to Controller - by anonymous5421 - 10-27-2019, 12:21 PM
RE: 403 error in AJAX to Controller - by InsiteFX - 10-28-2019, 03:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB