Correct way to pass data to Jquery Ajax

2019-09-02 19:22发布

Whats the correct way to pass data to ajax using jquery. I have the following method and I want to pass the CSRF token from a meta tag but it doesn't work.

<meta name="csrf-token" content="{{ csrf_token() }}">
<div class="fallback">
       <input type="file" name="logo" id="logo" class="inputfile"/>
</div>


$(document).on("change", ".fallback .inputfile", function() {
    $.ajax({
        url: "/upload",
        type: 'POST',
        cache: false,
        data:  {
            _token: $('meta[name="csrf-token"]').attr('content')
        },
        files: $(":file", this),
        iframe: true,
        processData: false
    }).complete(function(data) {
        console.log(data);
        // $('#img-thumb').attr('src', data.path);
        // $('input[name="job_logo"]').val(data.path);
    });
});

Laravel method to process the file:

public function upload(Request $request) {


    if($request->hasFile('logo')) {
        //upload an image to the /img/tmp directory and return the filepath.
        $file = $request->file('logo');

        $tmpFileName = time() . '-' . $file->getClientOriginalName();

        $tmpFilePath = '/img/tmp/';

        $file = $file->move(public_path() . $tmpFilePath, $tmpFileName);
        $path = $tmpFilePath . $tmpFileName;
        return response()->json(['path'=> $path], 200);
    } else {
        return response()->json(false, 200);
    }
}

I've followed the documentation from the following source https://cmlenz.github.io/jquery-iframe-transport/

I get tokenmismatch error. Note this is using Laravel 5.1

* UPDATE *

Should be able to add the token directly to data attribute as the csrf token is already in my meta tag. Below is an example done using backbone.js/ruby on rails, but I'm not an expert on backbone/rails so if any one can translate that into jquery it would be helpful. (http://estebanpastorino.com/2013/09/27/simple-file-uploads-with-backbone-dot-js/)

uploadFile: function(event) {
    var values = {};
    var csrf_param = $('meta[name=csrf-param]').attr('content');
    var csrf_token = $('meta[name=csrf-token]').attr('content');
    var values_with_csrf;

   if(event){ event.preventDefault(); }

    _.each(this.$('form').serializeArray(), function(input){
      values[ input.name ] = input.value;
   })

   values_with_csrf = _.extend({}, values)
   values_with_csrf[csrf_param] = csrf_token

   this.model.save(values, { iframe: true,
                          files: this.$('form :file'),
                          data: values_with_csrf });
}

2条回答
2楼-- · 2019-09-02 19:58
    processData: false

You've told jQuery to not convert the object containing your data into a format suitable for transmitting over HTTP.

查看更多
beautiful°
3楼-- · 2019-09-02 20:11

You need to add this to your page:

$(function() {

    $.ajaxSetup({ headers: { 'X-CSRF-TOKEN' : '{{ csrf_token() }}' } });

});

This is because the AJAX needs the X-CSRF-TOKEN everytime you send an AJAX request to the server (unless you turn it off, which I don't recommend).

SOURCE: my own experiences with Laravel.

查看更多
登录 后发表回答