React Native集成Perfect Money余额查询API时参数传递失败的问题求助
React Native集成Perfect Money余额查询API时参数传递失败的问题求助
我正在开发一个React Native应用,需要接入Perfect Money的支付功能,但发现他们没有提供JavaScript SDK,所以打算直接调用他们的API。
他们提供的PHP查询账户余额的方式我看到了,但我用Axios尝试做同样的操作时遇到了问题。我的代码是这样的:
const handlePaymentConfirmation = async () => { const apiUrl = "https://perfectmoney.com/acct/balance.asp"; const requestData = { AccountID: "1234", PassPhrase: "somePassphrase", }; try { axios .get(apiUrl, requestData) .then((response) => { console.log("Response:", response.data); }) .catch((error) => { console.error("API Error:", error); }); } catch (error) { console.error("Error updating user paid status", error); } };
调用后得到的响应是一段HTML错误提示:
Balance BalanceError: No values passed to API
想请教下,用JavaScript(React Native环境)该怎么正确调用这个API呢?
问题分析与解决方案
你遇到的核心问题是Axios的GET请求参数传递方式错误。Axios中get方法的第二个参数是配置对象,而非直接的参数对象,GET请求的参数必须放在配置对象的params字段中,否则参数不会被正确拼接到URL上,导致API无法识别你传递的AccountID和PassPhrase。
修改后的正确代码如下:
const handlePaymentConfirmation = async () => { const apiUrl = "https://perfectmoney.com/acct/balance.asp"; const requestParams = { AccountID: "1234", PassPhrase: "somePassphrase", }; try { // 将参数放入params字段,Axios会自动拼接到URL查询字符串中 const response = await axios.get(apiUrl, { params: requestParams }); console.log("Response:", response.data); } catch (error) { console.error("API Error:", error); console.error("Error updating user paid status", error); } };
额外提示
- 既然你用了
async函数,建议直接用await语法替代.then/.catch,代码会更简洁易读,也能统一捕获所有错误。 - 不要把PassPhrase这类敏感信息硬编码到代码里,建议从环境变量或安全的配置中心读取,避免泄露风险。
- 可以先在Postman或浏览器地址栏手动拼接参数(比如
https://perfectmoney.com/acct/balance.asp?AccountID=1234&PassPhrase=somePassphrase)测试API是否能正常返回结果,先排除API本身的问题。
备注:内容来源于stack exchange,提问作者Laspeed
相关产品推荐
相关产品推荐

