Posts

Showing posts with the label ajax

Fetching external RSS feeds with jQuery and Google Feed API

Sometimes, you just want to fetch the rss feeds from an external site without touching any server code. jQuery and Google Feed API will save your day. rss_parser.js: Then, in your html page or post, use this: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script> <script src="/path/to/parser.js"></script> <div id="myrss" class="box"></div> <script type="text/javascript">       $(function(){             parseRSS('http://mywebsite.com/feed/', '#myrss');       }); </script> The feeds will be inserted into myrss div. Reference:   https://developers.google.com/feed/v1/reference

Change background image randomly from a directory using Ajax in WordPress

A couple weeks ago, I'm working on a project that required me to develop a WordPress theme. One of the things that interested me was to create a block (html, in the front page) that change its background image randomly every time I reload the page. Here is how I did that: The main idea here is using Ajax to dynamically change background of a div tag to a random image loaded from a folder inside the theme's root every time I reload the home page. 1. functions.php add_action('init', 'enqueue_scripts_styles_init'); function enqueue_scripts_styles_init () { wp_enqueue_script( 'ajax-script', get_template_directory_uri().' /js/myscript.js ', array('jquery')); wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); } add_action('wp_ajax_get_rand_img', 'get_rand_img'); add_action('wp_ajax_nopriv_get_rand_img', 'get_rand_...

Cross-domain ajax request

Normally, if you send an ajax request to a server script stays in a different domain server, you will get this error: XMLHttpRequest cannot load http://my.external.domain/myscript.php . Origin http://my.external.domain is not allowed by Access-Control-Allow-Origin. So, to allow other domains call this cript, just set the 'access-control-allow-origin' header equal '*'. For example: http://my.external.domain/myscript.php <?php  header('content-type: application/json; charset=utf-8'); header('access-control-allow-origin: *'); $date = date('m-d-Y h:i:s a', time()); $json = json_encode($date); echo isset($_GET['callback'])     ? "{$_GET['callback']}($json)"     : $json; ?> And in my current site, send the ajax request to myscript.php: var mydate = ''; jQuery.ajax({ url: ' http://my.external.domain/myscript.php ', crossDomain: true, type: 'GET' }).done(...

Django & Ajax - Disable Django's Cross-Site-Request_Forgery protection on a view

This is an example about how to make an ajax call from a django template without applying the cross-site-request-forgery mechanism of Django, using  @csrf_exempt annotation. 1. Views from apps.testajax import models from django.shortcuts import render_to_response from django.template import RequestContext from django.views.decorators.csrf import ensure_csrf_cookie, csrf_exempt from django.http import HttpResponse import json def home(request): my_models = models.MyModel.objects.all() return render_to_response('home.html', locals(), context_instance=RequestContext(request)) @csrf_exempt def del_model(request): msg = 'Fail' if request.is_ajax(): if 'models_to_del' in request.POST.keys() and request.POST['models_to_del']: models_to_del_pks = request.POST['models_to_del'].split(',') if '' in models_to_del_pks: models_to_del_pks.remove('') models.MyModel.objects.filter(pk__in=models_...