Bypassing CORS, requesting public Market feeds

Bypassing CORS, using public Market feeds

I’ve been working on the CORS.ninja website for the last couple of months, it serves as an AJAX proxy to retrieve information from remote servers. All one needs to do is write an AJAX call to CORS.ninja with the URL and method parameters, and the return can either be JSON or JSONP.

In this example we will be retreiving data from Marketwatch’s NYSE Most Active at
http://www.marketwatch.com/tools/screener?exchange=Nyse&report=MostActive
and retrieving individual stock information from
http://www.marketwatch.com/investing/fund/SPY

var request_url = 'http://www.marketwatch.com/tools/screener?exchange=Nyse&report=MostActive';

function mostActive(data) {   
    var dom = $.parseHTML(data.body);
    var content = $(dom).find("#screener>table");  
    $("#holder").append(content);
};

$.ajax({
    type: 'GET',
    url: "http://www.cors.ninja/api/request",
    data: {
        url: request_url,
        method: "GET"
    },
jsonpCallback: 'mostActive',
contentType: "application/json",
dataType: 'jsonp'
}); 

The above by itself will pull the table and append it to the page. Now to get individual ticker information we can do something similar with a couple of different selectors.

var request_url = 'http://www.marketwatch.com/investing/fund/SPY';
function stockInfo(data) {   
    var dom = $.parseHTML(data.body);
    var name = $(dom).find("#instrumentname").text();
    var ticker = $(dom).find("#instrumentticker").text();
    var price = $(dom).find(".lastprice").text();  

    $("#holder").append("<span> " + name + " " + ticker  +  " " + price +"</span>" );
};

$.ajax({
    type: 'GET',
    url: "http://www.cors.ninja/api/request",
    data: {
        url: request_url,   
        method: "GET"
    },
    jsonpCallback: 'stockInfo',
    contentType: "application/json",
    dataType: 'jsonp'
}); 

Example can be found at
Cors.Ninja Stock Ticker Example
JS Fiddle Stock Ticker Example

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s