gpt4 book ai didi

javascript - 获取请求的 400(错误请求)参数是一个 url

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

我正在尝试执行带有参数的get请求,该参数实际上是一个字符串,但该字符串是一个url

所以我得到这个错误:

react-adal.js:50 OPTIONS https://abc-app.azurewebsites.net/api/Page/https://abcde.sharepoint.com/sites/Site1 400 (Bad Request)

我的脚本是这样的:

import React, { Component } from 'react';
import { Row, Col, Tabs, Menu, Dropdown, Button, Icon, message } from 'antd';
import PageHeader from '../../components/utility/pageHeader';
import Box from '../../components/utility/box';
import LayoutWrapper from '../../components/utility/layoutWrapper';
import ContentHolder from '../../components/utility/contentHolder';
import basicStyle from '../../settings/basicStyle';
import IntlMessages from '../../components/utility/intlMessages';
import { Cascader } from 'antd';
import { adalApiFetch } from '../../adalConfig';


export default class extends Component {
constructor(props) {
super(props);
this.state = {options:[]};
this.loadData = this.loadData.bind(this);
}

componentDidMount() {
adalApiFetch(fetch, "/SiteCollection", {})
.then(response => response.json())
.then(json => {
console.log(json);
const firstLevelOptions = json.map(post => ({
value: post.Url,
label: post.Title,
isLeaf: false
}));

this.setState({
options: firstLevelOptions
});
});
}

onChange = (value, selectedOptions) => {
console.log("value:", value, "selectedOptions", selectedOptions);
}

loadData = (selectedOptions) => {
console.log("loaddata", selectedOptions);

const targetOption = selectedOptions[selectedOptions.length - 1];
targetOption.loading = true;

const options = {
method: 'get',
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};

adalApiFetch(fetch, "/Page/"+targetOption.value, options)
.then(response => response.json())
.then(json => {
targetOption.loading = false;
console.log(json);
const secondLevelOptions = json.map(comment => ({
value: comment.id,
label: comment.body,
isLeaf: true
}));
targetOption.children = secondLevelOptions;
this.setState({
options: [...this.state.options],
});
}
);

};

render(){
const { rowStyle, colStyle, gutter } = basicStyle;
const TabPane = Tabs.TabPane;

return (
<div>
<LayoutWrapper>
<PageHeader>{<IntlMessages id="pageTitles.PageAdministration" />}</PageHeader>
<Row style={rowStyle} gutter={gutter} justify="start">
<Col md={12} sm={12} xs={24} style={colStyle}>
<Box
title={<IntlMessages id="pageTitles.siteCollectionsTitle" />}
subtitle={<IntlMessages id="pageTitles.siteCollectionsTitle" />}
>
<ContentHolder>
<Cascader
options={this.state.options}
loadData={this.loadData}
onChange={this.onChange}
changeOnSelect
/>
</ContentHolder>
</Box>
</Col>
</Row>
</LayoutWrapper>
</div>
);
}
}

我的 web api 代码是这样的:

 [Authorize]
public class PageController : ApiController
{
[HttpGet]
public async Task<ListItemCollection> Get(string sitecollectionUrl)
{
var tenant = await TenantHelper.GetActiveTenant();
var siteCollectionStore = CosmosStoreFactory.CreateForEntity<TenantManagementWebApi.Entities.SiteCollection>();
await siteCollectionStore.RemoveAsync(x => x.Title != string.Empty); // Removes all the entities that match the criteria
string domainUrl = tenant.TestSiteCollectionUrl;
string tenantName = domainUrl.Split('.')[0];
string tenantAdminUrl = tenantName + "-admin.sharepoint.com";

KeyVaultHelper keyVaultHelper = new KeyVaultHelper();
await keyVaultHelper.OnGetAsync(tenant.SecretIdentifier);

using (var context = new OfficeDevPnP.Core.AuthenticationManager().GetSharePointOnlineAuthenticatedContextTenant(sitecollectionUrl, tenant.Email, keyVaultHelper.SecretValue))
{
var pagesLibrary = context.Web.GetPagesLibrary();
CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
var pages = pagesLibrary.GetItems(query);
return pages;

};
}

}

最佳答案

调用 api 时,您必须对作为参数发送的 URL 进行编码:

adalApiFetch(fetch, "/Page/"+encodeURIComponent(targetOption.value), options)

而且您可能需要在后端对其进行解码。

关于javascript - 获取请求的 400(错误请求)参数是一个 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54501171/

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