【问题标题】:How to fetch response message from Laravel API?如何从 Laravel API 获取响应消息?
【发布时间】:2020-04-11 09:21:30
【问题描述】:

我在后端使用 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. Cannot read property of undefined

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

【问题讨论】:

    标签: laravel api post fetch


    【解决方案1】:

    试试这个

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

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

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

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

    【讨论】:

    • 虽然我宣布这个答案是正确的,但我还有一个问题。此方法在处理更新方法时工作正常......但是当您使用“发布”然后想要接收响应时,会出现如上所述的相同错误。 Possible Unhandled Promise Rejection: TypeError: Cannot read property 'json' of undefined
    猜你喜欢
    • 2019-04-25
    • 2023-04-04
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 2014-09-16
    • 1970-01-01
    相关资源
    最近更新 更多