【问题标题】:Sending JSON with array elements inside with axios.post使用 axios.post 发送带有数组元素的 JSON
【发布时间】:2020-11-16 17:15:15
【问题描述】:

.vue 文件中,我有这个方法,它以axios.post 调用Ruby 中参与的WebService URL 结束:

send_firma: function () {
                                        
    var person1 = {
        "identification": "30633685P",
        "name": "Leon Tolstoi",
        "email": "Leon.tolstoi@gmail.com"
    };
    
    var person2 = {
        "identification": "XCF762yh",
        "name": "Alex White",
        "email": "Alex.white@gmail.com"
    };

    var person_list = [];
    person_list[0] = person1;
    person_list[1] = person2;

    var formData = new FormData();
    formData.append("api_key", "90f4b51b5ac5");
    formData.append("signing_parties", person_list);
                
    var url = 'http://someplace.com:8090/someWS';

    var options = { headers: { 'Content-Type': 'application/json' } };
    
    axios.post(url, formData, options)
        .catch(e => {
            console.log("Send message failed!. " + e)               
    })
}

正如另一边的日志显示的那样,问题似乎是我无法正确发送person_list 数组。看那个[object Object]:

"signing_parties"=>"[object Object]",

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: json ruby vue.js axios


    【解决方案1】:

    您可以将其作为 json 字符串发送,并在后端将其加载为 json:

    formdata = new FormData()
    formData.append("signing_parties", JSON.stringify(person_list))
    
    axios.post(url, formData, options)
        .catch(e => {
            console.log("Send message failed!. " + e)               
    })
    

    【讨论】:

      【解决方案2】:

      尝试更改内容类型

      var options = { headers: { 'Content-Type': 'multipart/form-data' } };
      

      由于它是您要附加到 formData 的数组,因此请尝试替换它

      formData.append("signing_parties", person_list);
      

      有了这个

      person_list.forEach((person, index) => {
        formData.append('signing_parties' + '[' + index + ']', person)
      })
      

      【讨论】:

        猜你喜欢
        • 2016-10-06
        • 1970-01-01
        • 1970-01-01
        • 2021-02-15
        • 1970-01-01
        • 1970-01-01
        • 2019-03-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多