首页 > 解决方案 > 通过 superagent 在本地项目上响应 Ajax 请求

问题描述

我想通过 superagent 插件/库向存储在 backend/hi.php 中的 php 文件发送 AJAX 请求

这是php文件:

<?php
echo "hi";

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

这是reducers/index.js 中请求发生的reducer

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>↵&lt;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>↵&lt;/html>↵&quot;
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?

标签: javascriptreactjsajaxsuperagent

解决方案


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


推荐阅读