使用以下代码:
var form = new FormData(); form.append("active_registration_id", "123"); form.append("language_id", "79"); form.append("archived", "{{archived}}"); form.append("enabled", "{{enabled}}"); form.append("password", "SamplePassword"); form.append("password_confirmation", "SamplePassword"); form.append("registration_attributes[first_name]", "Justin"); form.append("registration_attributes[last_name]", "Trudeau"); form.append("registration_attributes[email]", "email@sample.com"); form.append("registration_attributes[telephone_1]", "555-555-5555"); form.append("registration_attributes[date_of_birth]", "1943-10-10"); form.append("registration_attributes[gender]", "Male"); form.append("registration_attributes[referral_code]", "Ample Clinic 123"); form.append("registration_attributes[status]", "Registration Pending"); var settings = { "async": true, "crossDomain": true, "url": "https://www.sample.com/v2/clients?token=DMfJjzWLngIn0JBHA0gWcg", "method": "POST", "headers": { "Content-Type": "application/x-www-form-urlencoded" }, "processData": false, "contentType": false, "mimeType": "multipart/form-data", "data": form } $.ajax(settings).done(function (response) { console.log(response); });
我们总是得到400(错误请求),但有更多错误:{“registration_attributes”:[“Missing”]} 此代码适用于Postman,但不适用于生产服务器。我如何创建registration_attributes数组有什么问题吗?
答案 0 :(得分:0)
FormData设置ajax请求的内容类型,删除您设置的内容类型标题。
P.S。 mimeType
不是jQuery ajax中的一个字段,只需删除它。