laravel - 如何从 Laravel API 获取响应消息?
问题描述
我在后端使用 Laravel,在前端使用 Aurelia。我知道这已经被回答过很多次了,但没有一个解决方案适合我。我想在 aurelia 中发回自定义消息并提取它们。我想做的事情:
//post api
.then(response => response.json())
.then(jsonData => {
if(jsonData.msg== true)
{
document.getElementById('close_add_new_modal').click();
} else {
console.log(`Error!`);
}
});
每次在这里我都会遇到异常:
Possible Unhandled Promise Rejection: TypeError: Cannot read property 'json' of undefined
后端:我如何尝试发送消息:
return response()->json(['msg' => 'true']);
为了解决我提到的这个错误: Possible Unhandled Promise Rejection。无法读取未定义的属性
紧随其后,但问题仍然存在。谁能告诉我我哪里错了或者获取自定义消息的正确方法是什么?
解决方案
尝试使用这个
return json_encode(['msg' => 'true']);
并尝试使用 aurelia-fetch-client 处理 api 请求
import `{HttpClient, json} from 'aurelia-fetch-client';
推荐阅读
- jenkins - 在 Jenkins 范围报告中,屏幕截图为空白
- udp - 如何处理丢失连接的客户端地址?
- python-3.x - 提取所有分页链接到带有scrapy for url = https://www.blablacar.in/ride-sharing/new-delhi/chandigarh/的页面
- css - 制作嵌套选择器以生成没有空格的选择器
- oracle - 在 Oracle 中插入多行
- facebook-graph-api - 反正有没有使用 Facebook v2.6 API 而不是最新的 v3.0 API?
- react-native - 在共享对话框中显示我的应用程序
- wordpress - 汇总自定义帖子字段中的特定元值
- powershell - PowerDesigner COM 对象在 Windows Server 2012 上为空
- javascript - 克隆的 DOM 结构上的 Angular 5 点击事件未触发