简体   繁体   English

Post Content-type undefined和application / json同时

[英]Post Content-Type undefined and application/json at the same time

I am trying to upload file and post some data at the same time. 我正在尝试上传文件并同时发布一些数据。 The value of topicSelected is java script object array. topicSelected的值是java脚本对象数组。 When I check in the c# controller the value of topicSelected is null. 当我检入c#控制器时,topicSelected的值为null。 When I check the value in my service everything is OK. 当我检查我的服务中的值时,一切正常。 I am thinking that the problem is that I am not specifying the application/json type. 我认为问题是我没有指定application / json类型。

setNewVideoRecord = function(file, videoName, videoVersion, topicSelected) {
    console.log(topicSelected);
    var self = this;
    var formData = new FormData();
    formData.append('file', file);
    formData.append('videoName', videoName);
    formData.append('videoVersion', videoVersion);
    formData.append('topicSelected', topicSelected);
    $http.post(self.baseUrl + "Admin/uploadVideoFile", formData, {
        withCredentials: true,
        headers: {
            'Content-Type': undefined
        },
        transformRequest: angular.identity
    }).then(function onSuccess(response) {
        self.fileNameUpload = null;
    })
}

You don't have an undefined content type. 您没有未定义的内容类型。 Telling your library that Content-Type is undefined just stops it trying to set its own default and let the underlying XMLHttpRequest object figure it out for itself. 告诉你的图书馆Content-Type是未定义的,只是停止尝试设置自己的默认值,让底层的XMLHttpRequest对象为自己找出它。 Since you are passing it a FormData object, it will examine that to identify the correct content-type (which is be multipart/mixed ). 由于您传递的是FormData对象,因此它会检查它以识别正确的内容类型( multipart/mixed )。

This will encode each piece of data (including the file you are uploading) as a separate part, each of which will have its own content-type set automatically. 这会将每个数据(包括您上传的文件)编码为一个单独的部分,每个部分都会自动设置自己的内容类型。

You can't make XHR encode the non-file data as JSON. 您无法使XHR将非文件数据编码为JSON。 It will use the standard multipart format with a separate part for each of the non-file inputs (and another part for the file). 它将使用标准的多部分格式,每个非文件输入都有一个单独的部分(文件的另一部分)。

You need to write the server side code to expect that format and not JSON. 您需要编写服务器端代码以期望该格式而不是JSON。

Since topicSelected is a complex data structure (and not a string) you can run it through JSON.stringify() before appending it to the form data object. 由于topicSelected是一个复杂的数据结构(而不​​是字符串),因此您可以将其附加到表单数据对象之前通过JSON.stringify()运行它。

Once you read the topicSelected string, you can parse it from JSON on the server. 阅读topicSelected字符串后,您可以从服务器上的JSON解析它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 npm请求同时发送令牌和标头“ content-type”:“ application / json” - npm request send token and header 'content-type': 'application/json' at the same time 无法发布内容类型:application / json从角度到导轨 - Cannot POST with content-type: application/json from angular to rails 应该什么时候Content-Type是application / json - When should Content-Type be application/json Fetch: Content-Type: application/json 在 post 方法中没有改变。 模式设置为 cors - Fetch: Content-Type: application/json not changing in post method. Mode set to cors AngularJS无法使用Content-Type:application / json发送post请求 - AngularJS can't send post request with Content-Type:application/json “axios.defaults.headers.common['Content-Type'] = 'application/json'” 但 axios.post 仍然是“application/x-www-form-urlencoded” - "axios.defaults.headers.common['Content-Type'] = 'application/json'" but axios.post still is "application/x-www-form-urlencoded" AJAX POST引发415不支持的媒体类型错误,并显示消息“请求必须具有“ Content-Type:application / vnd.api + json”” - AJAX POST throws a 415 Unsupported Media Type Error with message 'Request must have “Content-Type:application/vnd.api+json”' 使用 Fetch API javascript 将 Content-Type 设置为 application/Json - set Content-Type to application/Json using Fetch API javascript 无法将标题Content-Type更改为application / json - Unable to change header Content-Type to application/json 如何将表单更改为 Content-Type:application / json - How to change the form to Content-Type: application / json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM