Jquery+thinkphp method of implementing cross-domain Fetch data

Source: Internet
Author: User
In this paper, we describe a method for jquery+thinkphp to fetch data across domains. Share to everyone for your reference, as follows:

Today do a remote fetch data function, remember that jquery can be used for remote crawling Ajax, but not cross-domain. Find a lot of online. But I think it's a comprehensive one, so I now feel a bit complicated by the simple problem, but at least for now it's solved:

The effect of crawling data across domains to the local database and then updating asynchronously

I realized the way: jquery $.post send data to the server background, in the background of the PHP code to perform a remote crawl, stored in the database Ajax return data to the foreground, the front desk with JS to accept data and display.

Remote fetch gets data $ ("#update_ac"). Click (function () {      $username = $ ("#username"). Text ();      $ ("#AC, #rank, #Submit, #solved, #solved2, #solved3"). Ajaxstart (function () {        $ (this). HTML ("<span class=" Loading2 ' >   </span> ");      });      $.post ("update_ac/username/" + $username, {},function ($data) {        json = eval ("(" + $data + ")");        $ ("#Submit"). HTML (json.data.Submit);        $ ("#AC"). HTML (Json.data.AC);        $ ("#solved, #solved2, #solved3"). HTML (json.data.solved);        $ ("#rank"). HTML (Json.data.rank);       }      ), "JSON";});

The jquery code above is still on the four floor of the more clear, tangled my is that the JSON data received

JSON = eval ("(" + $data + ")"); the//eval () function computes a string and executes the JavaScript code in it.

In fact, this is still the foreground, cross-domain crawl is the extension of PHP Simple_html_dom completed (not clear can go to the Internet search, based on the development of PHP5)

Fetch the remote page to the local.

Import ("@.org.simple_html_dom"); in//thinkphp, you have to rename the code downloaded online to simple_html_dom.class.php to the appname\lib\org directory $html = file_ get_html (' http://openoj.awaysoft.com/JudgeOnline/userinfo.php?user= '. $username); Remote Fetch $ret = $html->find (' center ', 0)->plaintext; Return the data.

The code above is just the core code, and there are many more features of the simple_html_dom extension. Get to know it yourself.

The returned data is a string, and the regular expression is used to filter the required data.

It is hoped that this article is helpful to the PHP program design based on thinkphp framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.