简体   繁体   English

如何从Angular 4调用PHP函数

[英]How to call a PHP function from Angular 4

How to call a PHP function from angular 4? 如何从angular 4调用PHP函数? I have written the below code in my files and it returns nothing. 我在文件中编写了以下代码,但未返回任何内容。

    .component.ts
constructor(private _server:ServerConnectService) {..}
    $('#fileUpload').on('click', (e)=>
            {
              this._server.uploadImage("Testing..").done((res)=>{ 
                console.log("uploadImage Response "+res); // I need a result->"PHP -> uploadImage() calling " and data here
              });
          });


    .service.ts
export class ServerConnectService {
uploadImageUrl:string = "http://localhost/toolapp/uploadImage.php";
constructor(private _http:HttpClient) { 
  }

    uploadImage(d){
        var myFunction = 'uploadImage';
        var data = {"data":d};
        return $.post(this.uploadImageUrl+"?action="+myFunction,data).then((res)=>{ return res; })
    }


    uploadImage.php
    <?php
     header("Access-Control-Allow-Origin: *");
    function uploadImage($data){
        $res= "PHP -> uploadImage() calling ".$data;
        echo $res;
    }

    ?>

how to achieve this? 如何做到这一点?

Thanks, Guru 谢谢上师

您必须调用HTTP get或post api,然后才能使用php函数。

Angular is not server side rendered and hence the following code will not be parsed by the php engine to generate the required javascript. Angular不是服务器端渲染的,因此php引擎不会解析以下代码以生成所需的javascript。 You will have to expose a REST API with POST interface that accepts the image as a file or base64 encoded string. 您将必须公开具有POST接口的REST API,该接口将图像接受为文件或base64编码的字符串。

Then using Angular HttpClientModule or HttpModule you can send the image to the appropriate service. 然后,使用Angular HttpClientModule或HttpModule,可以将图像发送到适当的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM