开发者

Using jQuery and the Ajax method call, how do you query CraigsList

开发者 https://www.devze.com 2022-12-22 00:10 出处:网络
I was playing around with this earlier and failed to query craigslist for a page successfully. I would like to return the page from: h开发者_JAVA技巧ttp://sfbay.craigslist.org/art/

I was playing around with this earlier and failed to query craigslist for a page successfully.

I would like to return the page from: h开发者_JAVA技巧ttp://sfbay.craigslist.org/art/ using an ajax call, parse the html and process the results. For the purposes of this question, I am only interested in completing the query and retrieving the html.


You can go something like this:

<input type="button" id="btn" />

/* this will get the ajax response */
<div id="div_response"></div>

<script type="text/javascript">
$(function(){
  $("#btn").click(function(){
     $.ajax({
      url:'process.php',
      cache:false,
      success:function(response){
       $("#div_response").html(response);
      }
     });
  });
});
</script>

process.php (Note: I don't know which language you are going to use but in this example i show with php)

$contents = file_get_contents('www.craiglist.com');

// you do the parsing whatever
// finally send back the response

echo $contents;


You would need some code on the server that does the actual call to craigslist.org, as AJAX calls are limited to the same hostname. In other words, you cannot load a URL from craigslist.org directly via AJAX, but you need something on the server that does this, and in turn can be invoked via AJAX.


I don't think it's possible for query another site for security purposes. You can, however, do it with PHP.

Edit: Looks like you could make a request to this script instead, and pass it the craigslist URL as the get param proxy_url. Should give you an idea, anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消