简体   繁体   English

如何使用本地主机上的axios(react-redux)进行服务器调用(PHP)(使用快速服务器)?

[英]How to make a server call (PHP) using axios (react-redux) from localhost (using express server)?

I'm new to React. 我是React的新手。 I have a button in one of the components, on clicking the button, a server side (PHP) call should be made using axios GET call and the response should change the state of the store. 我在组件之一中有一个按钮,单击该按钮后,应使用axios GET调用进行服务器端(PHP)调用,并且响应应更改存储的状态。

I have set the app using webpack and using the express server. 我已经使用webpack和Express Server设置了应用程序。 What changes to their settings be made in order to run PHP code? 为了运行PHP代码,对其设置进行了哪些更改?

I have set the app using webpack and using the express server. 我已经使用webpack和Express Server设置了应用程序。 What changes to their settings be made in order to run PHP code? 为了运行PHP代码,对其设置进行了哪些更改?

You don't have to change any settings there. 您不必在那里更改任何设置。 These tools can't run PHP code. 这些工具无法运行PHP代码。 If you want to serve your static files ( .html , .css , .js ) with express you can leave that as it is. 如果您希望使用express服务静态文件( .html.css.js ),则可以保留原样。 But you have to run another local webserver next to express that can run PHP code and that listens on another port. 但是您必须在旁边运行另一个本地网络服务器来表示可以运行PHP代码并在另一个端口上进行侦听。 PHP 5.4+ has a built-in development server. PHP 5.4+具有内置的开发服务器。 Or you can use nginx or apache . 或者,您可以使用nginxapache

You could use eg Laravel which is a popular web application framework. 您可以使用Laravel等流行的Web应用程序框架。 This makes it easy to run a local development server and to define a RESTful api which you will need to provide in order to communicate with your react app. 这使得运行本地开发服务器和定义RESTful API变得很容易,您需要提供该API才能与react app进行通信。 Laravel uses the built-in PHP web server. Laravel使用内置的PHP Web服务器。 When you installed Laravel you can start it with: 当您安装Laravel时,可以使用以下命令启动它:

php artisan serve

This command will start a development server at http://localhost:8000 . 该命令将在http://localhost:8000启动开发服务器。 Express is per default running at http://localhost:3000 . Express默认情况下运行在http://localhost:3000

Then you can define a REST api in Laravel and send requests to it. 然后,您可以在Laravel中定义REST api并将请求发送给它。

Of course you can use other PHP frameworks too if you don't want to use Laravel. 当然,如果您不想使用Laravel,也可以使用其他PHP框架。

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

相关问题 如何使用php将mysql DB从本地主机复制到服务器? - how to copy mysql DB from localhost to server using php? 为什么php从本地主机发送电子邮件而不是使用PHPMailer从服务器发送? - Why php is sending email from localhost not from server using PHPMailer? 使用php从本地主机发送数据到Web服务器 - Data send to web server from localhost using php 使用PHP将MySQL从本地主机还原到实时服务器 - Restoring MySQL from localhost to live server using PHP 如何使用php读取保存​​在服务器上的文件并使用ajax调用该php页面以显示文件列表 - how to read files saved on the server using php and make a call to that php page using ajax to display the list of files 我如何使用zend服务器在本地主机上运行.php? - How do i run .php on localhost using zend server? 在本地主机和服务器上使用php创建备份 - creating a backup using php on localhost and server 从 Node API 到 PHP 服务器的 Axios 调用导致套接字挂断 - Axios call from Node API to PHP server leads to socket hangup 如何使用Blackberry Simulator从本地服务器获取数据 - How to get data from localhost server using Blackberry Simulator 如何使用Express函数从JavaScript页面调用PHP页面 - How to call a PHP page from a javascript page using express functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM