我有一个 React 组件,当一个复选框被按下时,它会调用一个 rest api,使用一个参数发布请求。
我在 webapi 中放置了一个断点,但它从未命中,我仍然在组件上得到 415 不受支持的媒体类型
react js component (see onchange event)
import React, { Component } from 'react';
import { Table, Radio} from 'antd';
import { adalApiFetch } from '../../adalConfig';
import Notification from '../../components/notification';
class ListTenants extends Component {
constructor(props) {
super(props);
this.state = {
data: []
};
}
fetchData = () => {
adalApiFetch(fetch, "/Tenant", {})
.then(response => response.json())
.then(responseJson => {
if (!this.isCancelled) {
const results= responseJson.map(row => ({
key: row.ClientId,
ClientId: row.ClientId,
ClientSecret: row.ClientSecret,
Id: row.Id,
SiteCollectionTestUrl: row.SiteCollectionTestUrl,
TenantDomainUrl: row.TenantDomainUrl
}))
this.setState({ data: results });
}
})
.catch(error => {
console.error(error);
});
};
componentDidMount(){
this.fetchData();
}
render() {
const columns = [
{
title: 'Client Id',
dataIndex: 'ClientId',
key: 'ClientId'
},
{
title: 'Site Collection TestUrl',
dataIndex: 'SiteCollectionTestUrl',
key: 'SiteCollectionTestUrl',
},
{
title: 'Tenant DomainUrl',
dataIndex: 'TenantDomainUrl',
key: 'TenantDomainUrl',
}
];
// rowSelection object indicates the need for row selection
const rowSelection = {
onChange: (selectedRowKeys, selectedRows) => {
if(selectedRows[0].key != undefined){
console.log(selectedRows[0].key);
const options = {
method: 'post',
body: JSON.stringify({ clientid : selectedRows[0].key.toString() }) ,
config: {
headers: {
'Content-Type': 'application/json'
}
}
};
adalApiFetch(fetch, "/Tenant/SetTenantActive", options)
.then(response =>{
if(response.status === 200){
Notification(
'success',
'Tenant set to active',
''
);
}else{
throw "error";
}
})
.catch(error => {
Notification(
'error',
'Tenant not activated',
error
);
console.error(error);
});
}
},
getCheckboxProps: record => ({
type: Radio
}),
};
return (
<Table rowSelection={rowSelection} columns={columns} dataSource={this.state.data} />
);
}
}
export default ListTenants;
和webapi方法
[HttpPost]
[Route("api/Tenant/SetTenantActive")]
public async Task<IHttpActionResult> SetTenantActive([FromBody]string clientid)
{
var tenantStore = CosmosStoreFactory.CreateForEntity<Tenant>();
var allTenants = await tenantStore.Query().Where(x => x.TenantDomainUrl != null).ToListAsync();
foreach(Tenant ten in allTenants)
{
ten.Active = false;
await tenantStore.UpdateAsync(ten);
}
var tenant = await tenantStore.Query().FirstOrDefaultAsync(x => x.clientid == clientid);
if (tenant == null)
{
return NotFound();
}
tenant.Active = true;
var result = await tenantStore.UpdateAsync(tenant);
return Ok(result);
}
最佳答案
我注意到几件事。
- 你正在尝试做一个
POST
带有 JSON 正文的请求。在客户端,您的请求看起来没问题。
据我了解,POST 正文是
{ clientid: 'some-client-id' }
- 有趣的是在您收到它的 Web API 中
public async Task<IHttpActionResult> SetTenantActive([FromBody]string clientid)
这可能是罪魁祸首。您的 API 需要一个字符串作为 POST 主体,它是一个 json 对象。您是否尝试过将类型更改为 dynamic
或 JObject
?
所以,本质上,
public async Task<IHttpActionResult> SetTenantActive([FromBody]dynamic clientRequest)
或
public async Task<IHttpActionResult> SetTenantActive([FromBody]JObject clientRequest)
或者,
如果您想继续按原样使用您的 API,那么您只需将您从客户端发出的请求更改为 ’some-client-id’
而不是 { clientid: 'some-client-id' }
关于javascript - 415(不支持的媒体类型)与 REST 发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51755025/