如何通过对graphQl服务的ajax post请求传递对象数组

时间:2017-06-27 07:38:13

标签: arrays ajax object graphql

这里我的数组格式是

coordinates : [ { divId:"1234", divCor: { divleft:"1223", divtop: "455" }, imgCor: { imgleft:"78895", imgtop: "452"
} }, { divId:"1234", divCor :{ divleft:"1223", divtop: "455", }, imgCor :{ imgleft:"78895", imgtop: "452",
} }
]

我的ajax请求是

`

  var data = "mutation M{publishProduct(coordinates: "+this.state.coordinates+" ){_id}}";

    console.log("publishProduct: "+JSON.stringify(data));

    $.ajax ({
      type: "POST",
      url: "/",
      contentType: "application/graphql",
      data: data,
      dataType : 'json',
      success:(data) => {

`

控制台我将上面显示为

publishProduct: "mutation M{publishProduct(coordinates: [object Object],[object Object],[object Object] ){ _id}} // Before passing data `

它没有以我需要的格式传递。

请帮我介绍如何为graphQl请求传递对象数组。

1 个答案:

答案 0 :(得分:1)

在将坐标连接到突变字符串之前,您必须对坐标进行JSON.stringify:

var data = "mutation M{publishProduct(coordinates: "+JSON.stringify(this.state.coordinates)+" ){_id}}";