功能未按预期工作(function isn't working as expected)
我正在开发一个接受2个参数的函数,
$base
和$top
我希望返回
$base
的功率,该功率将小于$top
和exponent。我期待的输出是
$power=125
和$exp=3
。我已经设法让
$exp
正确,但是我的6$power
正在上升,我无法弄清楚为什么它会超过$top
。这是我的代码:
<?php $base = 5; $top = 130; function topPower($base, $top) { $exp = 0; while($power<$top) { $exp++; $power = pow($base, $exp); } return array($power, $exp-1); } $results = topPower($base, $top); print_r ($results); ?>
I am working on a function that accepts 2 parameters,
$base
and$top
I am looking to return the power of the
$base
which would be less than$top
and the exponent.The outputs that i am expecting is
$power=125
and$exp=3
.I've managed to get
$exp
correct, but my$power
is coming up as 625 and I cannot figure out why it is going over$top
.Here is my code:
<?php $base = 5; $top = 130; function topPower($base, $top) { $exp = 0; while($power<$top) { $exp++; $power = pow($base, $exp); } return array($power, $exp-1); } $results = topPower($base, $top); print_r ($results); ?>
原文:https://stackoverflow.com/questions/20476306
最满意答案
大多数单页应用程序将所有请求路由到根路径,并让前端路由器接管。 我怀疑这是你的应用程序发生了什么。
您的后端代码或任何服务器配置代码中是否有任何形式的请求重定向逻辑?
您可以做的是将一些您不希望前端路由接管的路径列入白名单,例如以/ api开头的路径。 在这里粘贴服务器端配置将很有帮助。
在您的服务器配置中,当
inDev
为false
,您有一个app.get('*', ...)
来捕获所有请求并使用静态单页面应用程序进行响应。 因此API请求也会给出相同的响应。 您需要在通配符*
之前重构您的路由以匹配/api
。 这里可以找到一些例子Most single page applications route all requests to the root path and let the front end router take over. I suspect that is what is happening to your app.
Do you have any form of requests redirection logic in your back end code or any server configuration code?
What you can do is to whitelist some paths that you don't want front end routing to take over, such as those that start with /api. Pasting your server side config here will be helpful.
In your server config, when
inDev
isfalse
, you have aapp.get('*', ...)
that catches all requests and responds with the static single page app. Hence API requests will also give the same response. You will need to restructure your routes to match/api
before the wildcard*
. Some examples can be found here
相关问答
更多-
在React中处理Axios错误(Handling Axios error in React)[2023-11-25]
getAllPeople函数已经从您的axios调用返回数据或错误消息。 因此,在componentDidMount ,您需要检查对getAllPeople的调用的返回值,以确定它是错误还是返回的有效数据。 componentDidMount() { getAllPeople().then(response => { if(response!=error) //error is the error object you can get from the axios call ... -
如何通过反应js和axios从wordpress获取API调用?(How to get API call from wordpress with react js and axios?)[2022-04-19]
好的发现了它。 根本不需要映射步骤,数组已经在res中返回。 更换: const posts = res.data.map(obj => obj.data); this.setState({posts}); 有: if (res && res.data) { this.setState({ posts: res.data }); } Ok found it. You don't need the map step at all, the array is already ... -
您正在返回一个对象而不是组件或字符串。 改变这个; const listProducts = this.state.data.map((product) =>
- {product}
); 对此 // you need to set key to a unique string like id const listProducts = this.state.data.map((product) =>大多数单页应用程序将所有请求路由到根路径,并让前端路由器接管。 我怀疑这是你的应用程序发生了什么。 您的后端代码或任何服务器配置代码中是否有任何形式的请求重定向逻辑? 您可以做的是将一些您不希望前端路由接管的路径列入白名单,例如以/ api开头的路径。 在这里粘贴服务器端配置将很有帮助。 在您的服务器配置中,当inDev为false ,您有一个app.get('*', ...)来捕获所有请求并使用静态单页面应用程序进行响应。 因此API请求也会给出相同的响应。 您需要在通配符*之前重构您的路由以匹配/api ...嗯,这不是真正的“卷曲请求”。 这是一个HTTP请求。 Curl只是用于通过命令行执行HTTP(和其他)操作的工具。 在你的HTTP请求中,我可以看到你正在使用axios.get() ,但是你正在尝试发布一个post请求(你有一个你想要发送的数据对象)。 所以你应该使用axios.post() 。 最好查看axios页面以查看HTTP帖子的语法,包括如何在帖子中包含数据和标题对象。 在回答你的第二个问题时,是的,你可以。 在你的第一个axios帖子的.then .then()部分,你可以使用响应做另一个ax ...Django React Axios(Django React Axios)[2021-12-18]
管理通过更改我发布到的url( url:'/en/api/endpoint/' )修复它,因为显然是为了POST请求: 您通过POST调用此URL,但URL不以斜杠结尾,并且您设置了APPEND_SLASH。 在保持POST数据时,Django无法重定向到斜杠URL。 将表单更改为指向127.0.0.1:8000/en/api/endpoint/(注意斜杠),或在Django设置中设置APPEND_SLASH = False 之后我开始获得Forbidden 403 ,但是添加: from django.v ...