首页 > 解决方案 > 如何从 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。无法读取未定义的属性

紧随其后,但问题仍然存在。谁能告诉我我哪里错了或者获取自定义消息的正确方法是什么?

标签: laravelapipostfetch

解决方案


尝试使用这个

return json_encode(['msg' => 'true']);

并尝试使用 aurelia-fetch-client 处理 api 请求

import `{HttpClient, json} from 'aurelia-fetch-client';

https://aurelia.io/docs/plugins/http-services/


推荐阅读