首页 文章

SyntaxError:位于0的JSON中的意外标记<Angular Post Request to PHP

提问于
浏览
1

我正在使用角度2,我正在尝试将数据从角度通过发布请求发送到我的服务器上的php文件 . 当我尝试时,我收到此错误“SyntaxError:UENpected token <在位置0的JSON中” . 这是我到目前为止的代码 . 任何援助将不胜感激 . 谢谢!

App component button

<button class="btn btn-primary btn-lg" (click)="postData()"> Make Post Request </button>

app component code 我使用此功能并在模板中的按钮上调用它以发送请求

postData(){
this.requestService.postSomeData()
  .subscribe(
    data => this.postRequest = data,
    error => console.log('There is an error: ' + error),
    () => console.log("Completed Post Request!")
  );
}

Request Service code - 这是我在应用程序组件中调用的服务

postSomeData(){
   let url = 'link to php file here';
   let jsonData = {
       name: 'my Name'
   };
   let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

    return this.http.post(url, jsonData, {headers: headers})
          .map(res => res.json());
 }

PHP文件

<?php

 $postdata = file_get_contents("php://input");
 $request = json_decode($postdata);
 $name = $request->name;
 echo "Name: ".$name;

 ?>

1 回答

  • 0

    问题来自于PHP(服务器端我假设)没有返回响应 .

相关问题