传递给API时出现JSON ENCODED ARRAY问题

时间:2017-06-18 16:15:30

标签: javascript jquery json ajax

我尝试通过邮递员连接API enter image description here,它运行正常。附加屏幕:

以下是提供错误的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\"]"},

问题似乎在amenitie上,因为后端开发人员parmeter应该在代码中提及。他需要在JSON ENCODED ARRAY中获得价值。 返回错误是:

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

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

1 个答案:

答案 0 :(得分:-1)

contentType: 'application/json'表示您发送的数据类型,如果设置此标头,它将转换为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);
      }
});