0

As the title said I'm trying to post data but get the response "status":422,"error":"Unprocessable Entity". My code looks as follows:

    var data = {};
    var confirmData = {};
    confirmData['api_token'] = apikey;
    confirmData['absence'] = {};
    data['api_token'] = apikey;
    data['absence'] = {};
    data['absence']['absence_type_external_id'] = csv['absencetype'][i];
    if(csv['userid'][i] !== undefined){
        data['absence']['user_id'] = csv['userid'][i];
    } else {
        data['absence']['user_external_id'] = csv["externaluser"][i];
    }
    data['absence']['starts_at'] = getDate(csv["startdate"][i], "10");
    data['absence']['ends_at'] = getDate(csv["enddate"][i], "18");
    data['absence']['full_day'] = csv["additionalDate"][i] == "" ? true : false;
    $.ajax({
        url: "https://app.papershift.com/public_api/v1/absences",
        type: 'POST',
        dataType: 'application/json',
        headers: { 
            "Accept" : "application/json",
            "Content-Type": "application/json"
        },
        data: JSON.stringify(data),
        complete: function (rsp) {
           console.log(rsp.responseText);

If I'm trying the same code on My local xampp server everything works completely fine, but for security reasons I have to run it locally on a simple html file. If I do that I get the descibed error. I thought this might have something to do with the cross origin policy of my browser, so I started chrome with "--disable-web-security" and as that didn't work I tried different browsers and even a plugin for chrome that disabled the whole cross origin thing, but nothing really worked. I'm not even sure if that is the problem because I don't get any specific errors about it, just the 422. But I'm guessing the data I try to post isn't the problem because everything works just fine when I use it on My local server.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
ReMinD
  • 1
  • 2
  • 1
    422 basically means you are sending bad data. Have you looked at your request in Chrome and made sure it was what you were thinking? Are you sure the route you are sending is correct? – user1032531 Mar 05 '21 at 00:10
  • I checked everything and it looked good so far. I'm pretty sure that the data I'm sending is correct because as soon as I send it from a server everything works fine. But I don't know anything about headers and such so that's what could be wrong. Are there some headers I have to specify that I otherwise wouldn't need to, when I'm sending localy? – ReMinD Mar 06 '21 at 12:51

0 Answers0