0

I am trying to make a post request to my api which saves the data that I send through my request. I have tried all the solutions that I found here on SO but none worked. This is how I am sending the request through ajax:

var data={
    apiname: 'register', first_name: fname, last_name: lname, email_address: email,
    acc_pass: pass, coach_phone: phone, coach_dob: dob, profile_picture: myFile,
    coach_address: address, coach_state: state, coach_city: city, zip_code: zip, coach_bio: about,
    teaching_locations: teaching, playing_exp: playing_exp, teaching_exp: teaching_exp,
    private_lesson_rate: private_rate, group_lesson_rate: group_rate, certification: certifications,
    any_crime: 'No', us_citizen: 'Yes', allowed_contract_work: 'Yes', agree_terms: 'Yes',
    virtual_session: sessions
};
    
$.ajax({
    type: "POST",
    url: "my-url/signup_api.php",
    processData: false,            
    contentType: "application/x-www-form-urlencoded",     
    dataType: 'json',
    data: data,
    success: function (data2) {
        if (data2.status == '200') {
            alert(data2.message);

        }
        else {
            showError(data2.message, 3000);
        }

    },
    error: function (jqXHR, exception) {
        alert(exception);                                
    }
});

I don't want to send the data in a json way like json.stringify. The variables (fname, lname etc) are the input fields of my form and I am getting the values like this:

var fname = $("#first_name").val();

and same for all the other variables. And on my php side, I am trying to access the data like this:

if($_POST['apiname']=="register"){

//do stuff  

}

But my code never gets inside this if statement. Been stuck on this since last 2 days now.

sallu-tech
  • 77
  • 2
  • 10
  • 2
    Why are you setting `processData: false`? With that, you are _preventing_ jQuery from encoding the data as `application/x-www-form-urlencoded` for you, which it otherwise would have done automatically. – CBroe Mar 10 '21 at 10:53
  • @CBroe Because one of the field in my form is for profile picture where user uploads his picture so after research, I found that I need to add this line in order to send the file. Otherwise, if I remove this line, I was getting some kind of TypeError from jquery. – sallu-tech Mar 10 '21 at 10:55
  • [**data: JSON.stringify(data);**](https://stackoverflow.com/questions/40730233/when-to-use-json-stringify-in-an-ajax-call) try this too – Not A Bot Mar 10 '21 at 10:56
  • @NotABot like I have mentioned in my post that I don't want to stringify my data. – sallu-tech Mar 10 '21 at 10:57
  • If you want to perform an actual HTTP file upload, then `application/x-www-form-urlencoded` is the wrong content type to begin with. And setting `processData: false` will only prevent jQuery from encoding your data as _that_, but it will not automatically convert your data object into the _right_ format. And how exactly is that file represented in your `data` object anyway? – CBroe Mar 10 '21 at 11:03
  • @CBroe I am getting the file from the element and passing it in my ajax request parameters just like I mentioned in my post. I am getting the file like this: var myFile = $('#picture_field').prop('files')[0]; And then passing this "myFile" variable in my ajax request. – sallu-tech Mar 10 '21 at 15:31
  • That’s not gonna work, you need to use FormData. https://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously – CBroe Mar 11 '21 at 07:34

0 Answers0