Ajax 不将数据发布到服务器

时间:2021-03-10 10:48:49

标签: javascript php ajax

我正在尝试向我的 post 发出 api 请求,以保存我通过请求发送的数据。我已经尝试了在 SO 上找到的所有解决方案,但都没有奏效。 这就是我通过 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);                                
    }
});

我不想以 json 之类的 json.stringify 方式发送数据。变量(fname、lname 等)是我表单的输入字段,我得到如下值:

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

和所有其他变量相同。而在我的 php 方面,我正尝试像这样访问数据:

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

//do stuff  

}

但我的代码永远不会进入这个 if 语句。自过去 2 天以来一直被困于此。

0 个答案:

没有答案