doufen5175 2018-10-24 10:18
浏览 169
已采纳

Laravel / PHP-404路由返回不存在的页面

I'm working on a Laravel/ Angular application, and have just added a function to allow the user to set a custom addressee name when generating a PDF letter/ email via the provisional payment reminders page.

The Angular function is defined in provisional-reminders.ts with:

updatePreferredAddresseeDetails($event, payer) {
    console.log("updatePreferredAddresseeDetails() called ");
    console.log("$event: ", $event);
    console.log("taxpayer: ", payer);
    console.log("account.preferredAddressee: ", payer.preferredAddresseeName);

    //$event.currentTarget.cancelBubble = true;
    const contact = payer['contacts'][$event.currentTarget.selectedIndex]; /* Need to ensure contact is defined here, so it can be used below- currently null here */

    console.log("contact: ", contact);
    //const data = (<any>Object).assign({}, payer, { transactionContactId: contact.userId });
    //console.log("data: ", data);

    payer.loading = true;
    payer.originalAddresseeName = payer.addresseename;
    payer.originalAddresseeNamePdf = payer.addresseenamepdf;

    payer.ADDRESSEENAME = $event.contactPreferredName;
    payer.ADDRESSEENAMEPDF = $event.contactPreferredAddresseeName;
    /*contact.originalAddresseeName = payer.addresseename;
    contact.originalAddresseeNamePdf = payer.addresseenamepdf;

    contact.ADDRESSEENAME = $event.contactPreferredName;
    contact.ADDRESSEENAMEPDF = $event.contactPreferredAddresseeName; */

    console.log("payer.addresseename: ", payer.ADDRESSEENAME);
    console.log("payer.addresseenamepdf: ", payer.ADDRESSEENAMEPDF);
    //this.provService.updatePreferredAddresseeDetails(data).subscribe(
    this.provService.updateTransactionContact(contact).subscribe(
        (response:any) => {
            payer.addresseename = response.addresseename;
            payer.addresseenamepdf = response.addresseenamepdf;

            const message = new Message();
            message.type = MessageType.SUCCESS;
            message.message = 'Preferred Addressee details have been updated. ';
            this.messagingService.emitMessage(message);

            payer.loading = false;
        },
        (error:any) => {
            //reset the names back to what they were originally because saving failed
            payer.addresseename = payer.originalAddresseeName;
            const message = new Message();
            message.type = MessageType.ERROR;
            message.message = error.message || 'There was a problem updaing the preferred addressee details. If the problem persists, please contact us.';
            this.messagingService.emitMessage(message);

            payer.loading = false;
        }
    );
}

In the PHP controller, I have defined the function with:

public function updatePreferredAddresseeDetails(Request $request)
{
    try
    {
        DB::beginTransaction();

        $transactionContactId = $request->input('transactionContactId');
        $transactionItemId = $request->input('transactionItemId');

        if ($transactionItem = transactionItem::find($transactionItemId))
        {
            $transaction = $transactionItem->transaction;

            if (User::canAccessTransaction( auth()->user()->user, $transaction))
            {
                $transaction->savePropertyValueByPropertyTag('TRANSACTIONCONTACT', $transactionContactId);
                $account = Account::find($transaction->accountId);
                $account->savePropertyValueByPropertyTag('ADDRESSEENAME', $request->input('contactPreferredName'));
                $account->savePropertyValueByPropertyTag('ADDRESSEENAMEPDF', $request->input('contactPreferredAddresseeName'));

                $trasaction->save();
                $account->save();

                /*$newContact = User::find($transactionContactId); /*shouldn't need this line, as it's not a new contact- just updating an
                                                                    existing one*/
                DB::commit();

                return response()->json([
                    'success' => true,
                    'transactionItemId' => $transactionItem->transactionItemId,
                    'transactionId' => $transactionItem->transactionId,
                    'transactionContactId' => $transactionContactId, 
                    'addresseeName' => $account->ADDRESSEENAME,
                    'addresseeNamePdf' => $account->ADDRESSEENAMEPDF,
                    //'transactionContactName' => $newContact,
                    //dd(response);
                ]);
            }

            dd("transactionItem: ", $transactionItem);
        }
        else
        {
            dd("transactionItem could not be found ");
        }
    }
    catch(Excetpion $e)
    {
        dd("exception caught: ", $e);
    }
}

In the routes/web.php file, I have added the route to the prov group:

Route::group(['prefix' => 'prov'], function() {
    ...
    Route::post('/preferredAddressee', 'WebApi\ProvController@updatePreferredAddresseeDetails');
});

Currently, when the page loads, and I enter values into the fields that I have added to the form to be able to set/ amend the addresseename & addresseenamepdf values, when I tab out of those fields, this updatePreferredAddresseeDetails() function is called, but I get a message in the console that says:

POST ... 404 (Not Found)

unparsable response

and the Network-> Preview tab shows the message:

You've tried to access a page that doesn't exist.

Why is this? What am I doing wrong with the routing here?

  • 写回答

1条回答 默认 最新

  • dongwei4103 2018-10-24 14:42
    关注

    So it turns out this issue was happening because I had missed a couple of the 'parent' routes when setting the URL in the service.ts file... After changing that line from

    private preferredAddresseeDetailsUpdateUrl: string = BASE_URL + '/web-api/preferredAddressee';
    

    to

    private preferredAddresseeDetailsUpdateUrl: string = BASE_URL + '/web-api/t/prov/preferredAddressee';
    

    the routes were then mapped correctly, and I no longer got the page cannot be found error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化