samedi 9 juillet 2016

userfrosting post json with csrf


I am porting a web application to userfrosting I need to post a JSON data object. I am struggling with the csrf token.

The following code works fine with CSRF in userfrosting

    var postdata = {
      field1: 1,
      filed2: "data",
      csrf_token: $("meta[name=csrf_token]").attr("content")
    };

    $.ajax( { type: "POST", url: url, data: postdata, dataType: "json",
      success: ...,
      error: ...
    });

However, I need to post data as JSON and would like to use the following

    var postdata = {
      field1: 1,
      filed2: "data",
    };

    $.ajax( { type: "POST", url: url,
              data: JSON.stringify( postdata ), dataType: "json",
              contentType: "application/json; charset=utf-8",
      success: ...,
      error: ...
    });

How would I go about to add the csrf_token to get this working in userfrosting?


Aucun commentaire:

Enregistrer un commentaire