gpt4 book ai didi

javascript - 传递给 API 时的 JSON ENCODED ARRAY 问题

转载 作者:行者123 更新时间:2023-11-28 10:45:15 28 4
gpt4 key购买 nike

我尝试连接 API enter image description here通过 postman ,它工作正常。附屏幕:

下面是提供错误的ajax调用:

$.ajax({    
type: "POST",
url: "http://52.15.167.221:9000/addVenue",
dataType: 'json',
data: { deviceIdentifier: $('#identifier').val(),accessToken: $('#token').val(), deviceType: $('#tokenType').val(), numberOfGuests: $('#numGuests').val(), type: $('#spaceType').val(), category: $('#category').val(),spaceType: $('#spaceType').val(), pricePerHour: $('#pricePerHour').val(), address: $('#address').val(), numberOfSeats: $('#seats').val(), numberOfStanding: $('#standing').val(), amenities: "[\"dsdsds\",\"dsdsdd\"]"},

问题似乎出现在便利设施上,根据后端开发人员参数应该按照代码中提到的那样通过。他需要获取 JSON ENCODED ARRAY 中的值。返回错误是:

Object {status: false, message: "Add amenities in JSON string"}

我是这个 api 调用的新手,JQuery 请帮助我解决错误,因为我在代码中没有看到任何错误。

最佳答案

contentType: 'application/json' 表示您要发送的数据类型,如果设置此 header ,它将转换为 json 以 json 形式发送到服务器。

var JSONselected = JSON.stringify("[\"dsdsds\",\"dsdsdd\"]");
$.ajax({
type: "POST",
url: "http://52.15.167.221:9000/addVenue",
dataType: 'json', // this tell about the response type
contentType: 'application/json', // this tells what type you are sending
data: {
deviceIdentifier: $('#identifier').val(),
accessToken: $('#token').val(),
deviceType: $('#tokenType').val(),
numberOfGuests: $('#numGuests').val(),
type: $('#spaceType').val(),
category: $('#category').val(),
spaceType: $('#spaceType').val(),
pricePerHour: $('#pricePerHour').val(),
address: $('#address').val(),
numberOfSeats: $('#seats').val(),
numberOfStanding: $('#standing').val(),
amenities: JSONselected
},
success : function(response){
console.log(response);
}
});

关于javascript - 传递给 API 时的 JSON ENCODED ARRAY 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44616992/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com