React Ajax 通过 superagent 请求本地项目

React Ajax Request on local project via superagent

我想通过超级代理 plug-in/library 向存储在 backend/hi.php[=15 中的 php 文件发送 AJAX 请求=]

这是 php 文件:

<?php
echo "hi";

我基本上想从 php 文件中取回 hi。

这是 reducers/index.js 中发生请求的减速器

import superagent from 'superagent';
import jsonp from 'superagent-jsonp';

let initialState = null;

function reducer(state = initialState,action) {
    if(action.type=="SEARCH") {
        let url= "/backend/hi.php";
        superagent.get(url).end(function(err, res){
            console.log(res);
        });
    }

    else {
        return state;
    }
}

export default reducer;

我在控制台中得到的不是字符串“hi”:

    Response
1.  accepted: false
2.  badRequest: false
3.  body: null
4.  charset: "UTF-8"
5.  clientError: false
6.  created: false
7.  error: false
8.  forbidden: false
9.  header: {accept-ranges: "bytes", connection: "keep-alive", content-encoding: "gzip", content-type: "text/html; charset=UTF-8", date: "Tue, 21 Jul 2020 15:39:42 GMT", …}
10. headers: {accept-ranges: "bytes", connection: "keep-alive", content-encoding: "gzip", content-type: "text/html; charset=UTF-8", date: "Tue, 21 Jul 2020 15:39:42 GMT", …}
11. info: false
12. links: {}
13. noContent: false
14. notAcceptable: false
15. notFound: false
16. ok: true
17. redirect: false
18. req: Request {_query: Array(0), method: "GET", url: "/backend/hi.php", header: {…}, _header: {…}, …}
19. serverError: false
20. status: 200
21. statusCode: 200
22. statusText: "OK"
23. statusType: 2
24. text: "<!DOCTYPE html>↵<html lang="en">↵  <head>↵    <meta charset="utf-8" />↵    <link rel="icon" href="/favicon.ico" />↵    <meta name="viewport" content="width=device-width, initial-scale=1" />↵    <meta name="theme-color" content="#000000" />↵    <meta↵      name="description"↵      content="Web site created using create-react-app"↵    />↵    <link rel="apple-touch-icon" href="/logo192.png" />↵    <!--↵      manifest.json provides metadata used when your web app is installed on a↵      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/↵    -->↵    <link rel="manifest" href="/manifest.json" />↵    <!--↵      Notice the use of  in the tags above.↵      It will be replaced with the URL of the `public` folder during the build.↵      Only files inside the `public` folder can be referenced from the HTML.↵↵      Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will↵      work correctly both with client-side routing and a non-root public URL.↵      Learn how to configure a non-root public URL by running `npm run build`.↵    -->↵    <title>React App</title>↵  </head>↵  <body>↵    <noscript>You need to enable JavaScript to run this app.</noscript>↵    <div id="root"></div>↵    <!--↵      This HTML file is a template.↵      If you open it directly in the browser, you will see an empty page.↵↵      You can add webfonts, meta tags, or analytics to this file.↵      The build step will place the bundled scripts into the <body> tag.↵↵      To begin the development, run `npm start` or `yarn start`.↵      To create a production bundle, use `npm run build` or `yarn build`.↵    -->↵  <script src="/static/js/bundle.js"></script><script src="/static/js/1.chunk.js"></script><script src="/static/js/main.chunk.js"></script></body>↵</html>↵"
25. type: "text/html"
26. unauthorized: false
27. unprocessableEntity: false
28. xhr: XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: ƒ, …}
29. __proto__: Object

这是我的文件夹结构:

如何将 Ajax 请求正确发送到 hi.php?

我认为后端 api 正在返回 null 作为正文。这可能是您的 API 的问题。尝试从浏览器中点击 api 并查看它是否返回数据