search

Home  >  Q&A  >  body text

javascript - How to process the data returned by the server without submitting a get request with a custom xhr?

Premise: The server can already process this get request and return the required data.

Excuse me: If you use this code in the html page to make a get request to the server:

<a href="/data" id="a_click">请求一些数据</a>

SoAfter the get request is issued, how do I obtain and process the data returned by the server as soon as the data is returned?
(ps: That is to say, I don’t use JavaScript to submit the get request by creating an xhr object, but use the a tag to submit it)

Front-end noobs, please don’t complain if you are a master, thank you!

PHPzPHPz2756 days ago705

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-19 10:44:58

    A method similar to jsonp, except that jsonp is mainly used to implement cross-domain calls.

    I came here for a walk around while at work. I was short on time and did not deploy the server part.

    The principle is to use script tags and src attributes to set the interface address, let the server assemble the data, and return JavaScript code snippets.

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
    <script>
    function test(data) {
        console.log(data);
    }
    </script>
    <script src="./getData?callback=test"></script>
    </body>
    </html>

    Server returns data

    (function(callback) {
        var someData = {a: 1, b:2};
        callback(someData);
    })(test);

    Result

    reply
    0
  • Cancelreply