Advertisement
Guest User

Untitled

a guest
Dec 17th, 2011
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. /**
  2. * jQuery.ajax mid - CROSS DOMAIN AJAX
  3. * ---
  4. * @author James Padolsey (http://james.padolsey.com)
  5. * @version 0.11
  6. * @updated 12-JAN-10
  7. * ---
  8. * Note: Read the README!
  9. * ---
  10. * @info http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/
  11. */
  12.  
  13. jQuery.ajax = (function(_ajax){
  14.  
  15. var protocol = location.protocol,
  16. hostname = location.hostname,
  17. exRegex = RegExp(protocol + '//' + hostname),
  18. YQL = 'http' + (/^https/.test(protocol)?'s':'') + '://query.yahooapis.com/v1/public/yql?callback=?',
  19. query = 'select * from html where url="{URL}" and xpath="*"';
  20.  
  21. function isExternal(url) {
  22. return !exRegex.test(url) && /:\/\//.test(url);
  23. }
  24.  
  25. return function(o) {
  26.  
  27. var url = o.url;
  28.  
  29. if ( /get/i.test(o.type) && !/json/i.test(o.dataType) && isExternal(url) ) {
  30.  
  31. // Manipulate options so that JSONP-x request is made to YQL
  32.  
  33. o.url = YQL;
  34. o.dataType = 'json';
  35.  
  36. o.data = {
  37. q: query.replace(
  38. '{URL}',
  39. url + (o.data ?
  40. (/\?/.test(url) ? '&' : '?') + jQuery.param(o.data)
  41. : '')
  42. ),
  43. format: 'xml'
  44. };
  45.  
  46. // Since it's a JSONP request
  47. // complete === success
  48. if (!o.success && o.complete) {
  49. o.success = o.complete;
  50. delete o.complete;
  51. }
  52.  
  53. o.success = (function(_success){
  54. return function(data) {
  55.  
  56. if (_success) {
  57. // Fake XHR callback.
  58. _success.call(this, {
  59. responseText: data.results[0]
  60. // YQL screws with <script>s
  61. // Get rid of them
  62. .replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')
  63. }, 'success');
  64. }
  65.  
  66. };
  67. })(o.success);
  68.  
  69. }
  70.  
  71. return _ajax.apply(this, arguments);
  72.  
  73. };
  74.  
  75. })(jQuery.ajax);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement