Tag Archives: JSON

Python – Read and parse a JSON via URL

Here is an example Python program to read a JSON via URL using urllib2 and simplejson.

import urllib2
import simplejson

response = urllib2.urlopen("http://172.0.0.1:8000/custom/get/")
data = simplejson.load(response)
print data
# => {'content': 'Hello World!', 'success': True}

Continue reading Python – Read and parse a JSON via URL

Advertisements

Chrome – Bypass Access-Control-Allow-Origin on Mac

About a year ago, i posted an article about allowing the Javascript accessing the local file system in order to read the local JSON file.

 

So if you are using Mac, you need to quit the Chrome application. Then execute the following command in Terminal.

/usr/bin/open -a "/Applications/Google Chrome.app" --args --allow-file-access-from-files

 

Reference: Disable same origin policy in Chrome on Mac OSX

Chrome – Bypass Access-Control-Allow-Origin on local file system

Just wanna do some proof of concept on reading a JSON file in Javascript. So i open the index.html and read the JSON file locally. But the Chrome console throw the Access-Control-Allow-Origin error.

  • XMLHttpRequest cannot load file:///C:/Users/user/Desktop/readJson/data/file-1.json. Origin null is not allowed by Access-Control-Allow-Origin.

Although it should work if i upload those sources files to a web server, i would still make it work locally especially for development purpose. So here is a small trick which could ask the Chrome browser to bypass this cross-domain restriction.
Continue reading Chrome – Bypass Access-Control-Allow-Origin on local file system

Drupal 8 – Setup the RESTful Service @ 1

After studying Drupal 8 for a few days, i got the feeling that there are still a lot of works to polish it before a beta or RC release. And lacking documentation is another big problem to kick start with Drupal 8. This post may help you to get some simple idea on how to setup RESTful service in Drupal 8 but very likely, the details will change in the future.

1. Enable the RESTful Web Services.
drupal-8-setup-rest-service-1
Continue reading Drupal 8 – Setup the RESTful Service @ 1

Composer – Manage your PHP dependencies

Mess up with the PHP dependencies in different environments? Composer is a PHP dependency manager where all the dependencies information are stored in the JSON file called composer.json. Similar to the pom.xml if you are using Maven in Java.

This example is done on a Windows machine. You could refer to Composer website if you are using Mac or Linux.

1. Download and install the Composer as stated in the Composer website.
Continue reading Composer – Manage your PHP dependencies

jQuery & JSON – Make Cross Domain Request Using jQuery.getJSON() with JSONP

A few days ago i posted an article about making a JSON request using $.getJSON().
jQuery & JSON – Make JSON GET request using jQuery.getJSON()

But i have made a mistake there because i didn’t realize the Cross Domain problem. So this article shows you how to resolve the Cross Domain issue in $.getJSON() by JSONP. Continue reading jQuery & JSON – Make Cross Domain Request Using jQuery.getJSON() with JSONP

jQuery & JSON – Make JSON POST request using jQuery.post()

We can also make JSON POST request using the jQuery.post() function. If you want to use GET request. please refer to the previous post.
jQuery & JSON – Make JSON GET request using jQuery.getJSON()

 
Update @ 2011-10-13: This example does not work for Cross Domain POST request since the Current jQuery version(1.6.4) does not allow a Cross Domain POST request. For more information, you can refer to Mark Needham – jQuery: $.post, ‘jsonp’ and cross-domain requests

So if you want to make a Cross Domain GET request. Take a look on the following post.
jQuery & JSON – Make Cross Domain Request Using jQuery.getJSON() with JSONP

 
Here is the new server.php Continue reading jQuery & JSON – Make JSON POST request using jQuery.post()

jQuery & JSON – Make JSON GET request using jQuery.getJSON()

The following example makes a HTTP GET request with a JSON input and return the corresponding JSON object.

 
Update @ 2011-10-12: The example here does not support cross domain request. In other words, the server.php and json-get.php have to be inside the same domain. (ex. localhost) Thanks dskanth for pointing out the problem.
jQuery & JSON – Make Cross Domain Request Using jQuery.getJSON() with JSONP

 
Add the following 2 files to your web server
server.php Continue reading jQuery & JSON – Make JSON GET request using jQuery.getJSON()