亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

POSTMAN 上的 POST 請求成功,但 Angular 7 上沒有

POSTMAN 上的 POST 請求成功,但 Angular 7 上沒有

PHP
三國紛爭 2022-07-16 17:03:56
我已經關注了這個鏈接:Postman 'POST' request sucess but Angular 5 'Post' not working,并按照它來糾正我的問題,但有些問題沒有得到解決。它總是給我else block輸出,我會在 Angular 代碼中展示給你看。注意:為 POSTMAN 完美工作。可能的錯誤:我的 PHP 代碼中存在無法識別輸入的問題。我的 API 是一個 POST API,它接受一個參數,即phone_num,這是強制性的。從 Postman 傳遞參數適用于 api,但是當我通過 Angular 進行時,它不起作用并轉到else blockAPI 代碼。由于這是一個RAW PHP 代碼,所以我不知道如何將這個microsoft.aspnet.webapi.cors 包導入到我的 RAW PHP 文件中,或者它是否會解決這個問題。PHP代碼:<?php    include('/Applications/MAMP/htdocs/api/Requests/library/Requests.php');    $request_method = $_SERVER['REQUEST_METHOD'];    if ( $request_method == 'POST' ){            if(isset($_GET['phone_num'])){               echo $_GET['phone_num'];            }else{              echo 'No phone number found';            }    }else {        echo 'No defined function for this method. Please use POST only';    }?>對于 POSTMAN,我phone_num在控制臺/正文中顯示。但是對于 Angular,控制臺顯示:No phone number found這很奇怪。角代碼:import { Component } from '@angular/core';import { HttpClient, HttpHeaders } from '@angular/common/http';export class AppComponent {  message: string;  url: string = 'xxxxxxx';  customernum: number;  httpOptions = {    headers: new HttpHeaders({      'Content-Type': 'application/json',    })  };  constructor(private http: HttpClient){}  callMe(){    this.message = 'Please wait....'    this.http.post(this.url, JSON.stringify({ 'phone_num': this.customernum }), this.httpOptions).subscribe(      response => console.log(JSON.stringify(response)),      error => console.log(error)    );  }}我還檢查了this.customernum是否在控制臺中正確打印了數字,它正在控制臺中傳遞,Network Console在Chrome Dev.控制臺中的錯誤日志:
查看完整描述

5 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

您正在使用 $_GET 來檢索您的“POST”數據

嘗試$_POST['phone_num']安裝$_GET['phone_num']


查看完整回答
反對 回復 2022-07-16
?
阿波羅的戰車

TA貢獻1862條經驗 獲得超6個贊

為此,我個人要感謝兩個人,他們是:


Nijeesh Joshy用于排序的最大問題 METHOD to METHOD 與 PHP 腳本的矛盾。

布拉德解釋了正確的格式以使操作順利運行

調查結果:為了使這項工作正常工作,即$_POST工作 POST 方法,您需要在 header 中發送 application/x-www-form-urlencoded Content-Type。我不知道,但在 Content-Type 中發送數據是application/json行不通的。


解決方案:


1. SyntaxError: Unexpected token N =>這樣做是因為在 PHP 腳本中,它必須在 中返回json_encode(),這就是為什么它No phone number found, N at 0th position在 else 腳本中的此語句中返回此錯誤。


<?php

    include('/Applications/MAMP/htdocs/api/Requests/library/Requests.php');

    $request_method = $_SERVER['REQUEST_METHOD'];


    if ( $request_method == 'POST' ){

            if(isset($_POST['phone_num'])){

               echo $_POST['phone_num'];

            }else{

              echo json_encode('No phone number found');

            }

    }else {

        echo json_encode('No defined function for this method. Please use POST only');

    }

?>

2. $_POST 不適用于 Angular 代碼中的 POST 方法 =>已在上面解釋。


import { Component } from '@angular/core';

import { HttpClient, HttpHeaders } from '@angular/common/http';


export class AppComponent {

  message: string;

  url: string = 'xxxxxxx';

  customernum: number;

  httpOptions = {

    headers: new HttpHeaders({

      'Content-Type': 'application/x-www-form-urlencoded'

    })

  };


  body: any = `phone_num=${this.customernum}`;  


  constructor(private http: HttpClient){}


  callMe(){

    this.message = 'Please wait....'

    this.http.post(this.url, this.body, this.httpOptions).subscribe(

      response => console.log(JSON.stringify(response)),

      error => console.log(error)

    );

  }

}

如果您仍然遇到錯誤,那么您的角度代碼中會有一些格式錯誤。請點擊此鏈接。我希望這對你有幫助。我看到人們對使用非常嚴格$_POST[] in place of $_GET[],但這Content-Type就是問題所在。


查看完整回答
反對 回復 2022-07-16
?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

SyntaxError: Unexpected token N in JSON 意味著你有一些格式錯誤的 JSON,它通常是一個沒有用引號括起來的字符串。
1) 檢查 this.customernum 是否不為空或不包含任何壞字符。
2) 用雙引號將 phone_num 括起來。

查看完整回答
反對 回復 2022-07-16
?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

如果您使用的是表單而不是嘗試直接從該表單添加值,而不是任何額外的變量。用您的表單名稱替換此處的表單


this.http.post(this.url, { 'phone_num': this.form.value.phoneForm}, this.httpOptions).subscribe(

      response => console.log(JSON.stringify(response)),

      error => console.log(error)

    );


查看完整回答
反對 回復 2022-07-16
?
手掌心

TA貢獻1942條經驗 獲得超3個贊

傳遞原始對象{ 'phone_num': this.customernum },不要使用JSON.stringify()


試試這樣:


callMe(){

    this.message = 'Please wait....'

    this.http.post(this.url, { 'phone_num': this.customernum }, this.httpOptions).subscribe(

      response => console.log(JSON.stringify(response)),

      error => console.log(error)

    );

  }


查看完整回答
反對 回復 2022-07-16
  • 5 回答
  • 0 關注
  • 321 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號