gpt4 book ai didi

javascript - 将具有两个属性(数组和对象)的对象发布到 webapi? -asp.net webpi

转载 作者:行者123 更新时间:2023-11-28 08:35:36 25 4
gpt4 key购买 nike

我有一个包含两个对象的 JSON 对象

第一个对象是一个名为 customer 的 json 对象

第二个是一个名为 dataserves 的 JavaScript 数组

我一直在尝试以以下格式将这两个对象发布到 webapia,

//this is the webapi method

public CustomerAndDataServices POST(CustomerAndDataServices cd)
{
return cd;
}

我发布的 JSON 对象是这个

var customer ={name="clark"};

var dataservices=['email','internet', 'chat'];

var ds={''dataservices}

var data={

customer:customer

dataservices:ds

};

//this is how i post t the webapi

$.ajax({

url:'/api/myapi'

data:{cd:dataservices}

});

//问题是,当我发布此内容时,绑定(bind)发生在客户对象上,但数据服务数组为空。

我已经了解了如何通过将分配给数组的对象的属性设置为空字符串来将单个数组发送到 webapi

suhc为

$.ajax({

url:'/api/myapi'

data:{'':dataservices}

});

这个解决方案工作正常,但是它如何适用于我发布一个属性是对象和数组的 json 对象的场景?

谢谢

最佳答案

试试这个:

 var customer = { name: "clark" };
var dataservices = ['email', 'internet', 'chat'];

//convert javascript object to JSON
var cd = JSON.stringify({ customer: customer, dataservices: dataservices });

$.ajax({
url: '/api/myapi/',
//declare ajax call is an HTTPPost
method: 'POST',
data: cd,
//set data format as JSON
contentType: 'application/json'
});

关于javascript - 将具有两个属性(数组和对象)的对象发布到 webapi? -asp.net webpi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21252957/

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