Welcome Guest, Not a member yet? Register   Sign In
Cannot get AJAX to work for Post/Create
#15

(This post was last modified: 06-08-2022, 07:06 PM by spreaderman.)

Thanks and am often using the developer tools to console out debugging info.  I have now turned of csfr per below.  The controller and method sit under Admin.  I have removed the references to csfr and confirm it is working.

PHP Code:
public $globals = [
'before' => [
// 'honeypot',
'csrf' => ['except' => 
[
'Api/*''Admin/*'],
],
],
'after'  => [
 
'toolbar',
 
// 'honeypot',
 
],
]; 

When I submit now, I get [] in the console for " console.log(data);" and under the network tab I get;

headers ok 200
request url; https://development.example.com/admin/tasks/create
payload;  [object Object]
preview: []

So I guess I am not sending the form data, right?

This is again my js;

Code:
  $(function() {
        $('#form-task-create').on('submit', function(e) {
            //alert("Hello! I am an alert box!!");
            e.preventDefault();
            //OR can use var formData = new FormData(this);
            var formData = $(this);
            //var tokenHash=jQuery("input[name=csrf_token_mamamia]").val();
           
            //We can add more values to form data
            //formData.append("key", "value");
            baseurl = 'https://development.example.com'
           
            $.ajax({
                url:  'https://development.example.com/admin/tasks/create',
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: 'json',
                beforeSend: function (xhr)  {     
                    //xhr.setRequestHeader('X-CSRF-Token' , tokenHash);     
                },
                success: function(data) {
                    console.log(data);
                   
                    $("#resultDiv").html(data);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log(formData);
                    //console.log(errorThrown+'---'+textStatus+'---'+jqXHR);
                    alert('Error at add data');
                }
            });
        });
    });
Reply


Messages In This Thread
RE: Cannot get AJAX to work for Post/Create - by spreaderman - 06-08-2022, 07:05 PM



Theme © iAndrew 2016 - Forum software by © MyBB