Need content type for FormData

为君一笑 提交于 2021-02-11 15:02:30

问题


I am sending formData or Json to server. So i need contentType for formData. Please don't give false as contentType value.

formdata = new FormData(form[0]);
$.ajax({
    url: url,
    data: formdata? formdata :$(#User).serialize(),                 
    type: 'post',
    cache: false,
    contentType: "json",
    processData: false,
    beforeSend: function () {
        $(options.createOrUpdateRelationship).attr('disabled', 'disabled');
        var target = $(options.setupSubContainer);
        $("body").append(options.fadeoutDiv);
        options.spinner.spin(target[0]);
    },
    success: function (data) {

    }
});

回答1:


Need to give the dataType because in which format sending data to server for example json,array and you can give the formid.serialize() to to serialize the formadatas in a Query string format and send to server you can receive the data by $_POST in php

var form = $("#formId").serialize();
$.ajax({
type: 'POST'
url: url,
data: form,
dataType: 'json',
success: function (data) {
   //add your code
}
});

contentType removed - we are not sending JSON.



来源:https://stackoverflow.com/questions/27123238/need-content-type-for-formdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!