"jquery.ajax-cross-origin.min.js"
and copy it to your JavaScript folder.<script>
tag:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
tag after the jQuery <script>
tag:
<script type="text/javascript" src="http://www.example.com/path/getdata"></script>
Use jQuery.ajax()
:
$.ajax({ crossOrigin: true, url: url, success: function(data) { console.log(data); } });
Use jQuery.getJSON()
:
$.ajaxSetup({ crossOrigin: true }); $.getJSON(url, null, function(data) { $( '#test' ).html(data); });
Note: the only change from regular jQuery.ajax() call is the option: crossOrigin: true
Choose your Proxy Server.
You can choose one of the 3 options:Extract the file: "proxy.php"
and copy it to your server if your server support PHP (or you can write your own proxy script that accept url as param and return jsonp).
proxy.php:
<?php $url = (isset($_GET['url'])) ? $_GET['url'] : false; if(!$url) exit; $referer = (isset($_SERVER['HTTP_REFERER'])) ? strtolower($_SERVER['HTTP_REFERER']) : false; $is_allowed = $referer && strpos($referer, strtolower($_SERVER['SERVER_NAME'])) !== false; //deny abuse of your proxy from outside your site $string = ($is_allowed) ? utf8_encode(file_get_contents($url)) : 'You are not allowed to use this proxy!'; $json = json_encode($string); $callback = (isset($_GET['callback'])) ? $_GET['callback'] : false; if($callback){ $jsonp = "$callback($json)"; header('Content-Type: application/javascript'); echo $jsonp; exit; } echo $json; ?>
Use jQuery.ajax()
:
$.ajax({ crossOrigin: true, proxy: "http://www.domain.com/path/proxy.php", //to overide default proxy url: url, success: function(data) { console.log(data); } });
Use jQuery.getJSON()
:
$.ajaxSetup({ crossOrigin: true, proxy: "http://www.domain.com/path/proxy.php" }); $.getJSON(url, null, function(data) { $( '#test' ).html(data); });
"code.gs"
."code.gs"
into the new Script.Save the Script, set the version and Publish.
"jquery.ajax-cross-origin.min.js"
and set the proxyJsonp variable to your web app URL.