Showing posts with label web services. Show all posts
Showing posts with label web services. Show all posts

Friday, September 28, 2012

Jquery cross domain AJAX requests and HTML parsing list of image src using JQUERY MAP

Recently I have been working with Wikipedia, Wikimedia and had to do AJAX requests and parsing the returned HTML.

Javascript and JQUERY have problems when you try an AJAX query to cross-domain sites. I read the following article by Micahs

Making Cross Domain jQuery AJAX Calls and followed the method suggested using YQL and seems to work fine. I have repeated below the code mentioned in the article for reference.

//feel free to add querystring vars to this
var myurl="http://www.example.com/get-post.ashx?var1=var1value&callback=?";
//make the call to YQL
$.getJSON("http://query.yahooapis.com/v1/public/yql?"+
"q=select%20*%20from%20html%20where%20url%3D%22"+
encodeURIComponent(myurl)+
"%22&format=xml'&callback=?",
function(data){
if(data.results[0]){
//this data.results[0] is the return object you work with,
//if you actually want to do something with the returned json
alert(data.results[0]);
} else {
var errormsg = '
Error: could not load the page.
'
;
//output to firebug's console
//use alert() for other browsers/setups
console.log(errormsg);
}
}
);



I have given below an example of parsing all the image source in a list of images.



The list item code snippet is given below



    <div class="thumb" style="width: 150px;"><div style="margin:15px auto;"><a href="//commons.wikimedia.org/wiki/File:Adrien_Ysenbrandt_-_Virgin_and_Child_with_Two_Angels_in_a_Landscape_-_Walters_37266.jpg" class="image"><img alt="Adrien Ysenbrandt - Virgin and Child with Two Angels in a Landscape - Walters 37266.jpg" src="//upload.wikimedia.org/wikipedia/commons/thumb/4/49/Adrien_Ysenbrandt_-_Virgin_and_Child_with_Two_Angels_in_a_Landscape_-_Walters_37266.jpg/102px-Adrien_Ysenbrandt_-_Virgin_and_Child_with_Two_Angels_in_a_Landscape_-_Walters_37266.jpg" width="102" height="120" /></a></div></div>


After alert(data.results[0]); you can do something like the following to get the  thumb html in the list:



 var thumb_array_img = $(('.thumb img'), result);


To get all the src links in an array use the JQUERY map function like the following code:



image_thumb_array = [];


image_thumb_array = $(thumb_array_img).map(function ()

                   { return $(this).attr("src"); });

Wikimedia Wikipedia parsing data and machine readable code useful links

The Wikipedia, Wikimedia API has lot of information but not well documented. I have been working with that to get some data and parse it. I found the following links very helpful.

  1. API:Main page
  2. MediaWiki API documentation page
  3. API sandbox
  4. Commons:Machine-readable data
  5. Category:Infobox templates
  6. What is the Full URL of the images returned by a wikipedia query...
  7. API:Query
  8. Call Wikipedia API using jQuery
  9. Manual:Parameters to index.php
  10. Template:Information
  11. API:Properties
  12. API:Lists

Wikipedida Wikimedia query returns 403 forbidden

I had the following webclient request 
 WebClient serviceRequest = new WebClient();
var JsonResponse = serviceRequest.DownloadString(new Uri(urselected));

Where urselected was the REST query string. The query worked fine in my browser but returned 403 forbidden error. I found the following post which addresses the problem:

Wikipedia query returns error 403

Modified my code as follows and it worked find:

WebClient serviceRequest = new WebClient();
           serviceRequest.Headers.Add("user-agent", "Silver Azure");

           var JsonResponse = serviceRequest.DownloadString(new Uri(urselected));

Saturday, November 20, 2010

Parsing Json using C# dynamic

I had an application where I had to get data from  a web service. Get some name data from that service and then get relevant images from another web service. I was wondering how to do that because my application is MVC2 with Javascript and JQuery.
My first approach was to try to get the results  from first web service in the view page,  manipulate the result with JQuery, extract the information and then make the second web service call. However, I thought it was clumsy.
Then I came across JavascriptSerializer. But it required that I declare a complete class for the Javascript object which I was reluctant to do. Then I looked at Json.NET. Elegant solution but I thought was an overkill for what I wanted to do.
Then I came across C# 4.0, Dynamic Programming and JSON  by Nikhil which is also great, but again for my application was an overkill.
Finally I hit pay dirt when I saw this great article  Using C# 4.0 and dynamic to parse JSON by Shawn which is wonderful. Excellent piece of code to extract what I wanted. Almost like working with Javascript in C#. I had to do the minor  bug correction by Drew mentioned in serialize JSON into C# dynamic object?.
I used the code and I was able to scan through the objects like I would do in Javascript notation.
Excellent tool to have in your arsenal when you want to work with web services which returen JSON in MVC.

Tuesday, September 28, 2010

Links to various US recreation sites

  1. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllActivities
  2. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllElements
  3. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllFacilities
  4. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllFacilityActivities
  5. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllFacilityAddresses
  6. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllFacilityElementsForOrgID
  7. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllFacilityEvents
  8. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllOrganizations
  9. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllOrgFacilities
  10. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllOrgRecAreas
  11. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllRecAreaActivities
  12. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllRecAreaAddresses
  13. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllRecAreaAdvisories
  14. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllRecAreaElementsForOrgID
  15. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllRecAreaEvents
  16. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllRecAreaFacilities
  17. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllRecAreas
  18. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllRecAreasForOrgID
  19. http://recdata.gov/RIDBWebService/RIDBService.jws/getAllRecElementsForOrgID
  20. http://recdata.gov/RIDBWebService/RIDBService.jws/getRecGovFacilities