Parsing data from response to a variable

Hi all,

I’m at witts end. I originally got this working using a regex php file and returning the data via ajax with no problems. But each time there was a link on the page it requests that php file, and with many links it returned many iterations of the page and many requests it just isn’t fantastic.

An example of the data I’m looking at parsing is here:

http://www.wowhead.com/spell=104389&power

You can see when visiting this page, it looks to me like a javascript function.

I am able to successfully get this data response by looking at ‘Network’ in Google Chrome Developer Tools, but I’m unsure how to parse it from there to my js file so I can insert it into my html.

Here is my current jquery:

$(document).ready(function(){

	$("a[href*='www.wowhead.com/spell'] , a[href*='www.wowhead.com/achievement']").each(function(){
		
		var wowheadlink = $(this).attr('href') + '&power';
		
		var iconaddy = null;
		
		$.ajax({
		  url: wowheadlink,
		  type: 'get',
		  dataType: 'jsonp',
		  context: this,
		  success: function(data){
               console.log(iconaddy);
			}
			
		});
		
		
	});	;	
	
});

At the moment I’m just looking at any kind of data to be displayed in the console. Any ideas?

Cheers.

Currently you’re logging the iconaddy variable which will always return null, instead what you want to be doing is logging the data parameter been set in your anonymous function for success. To set the variable you can simply assign the value of data to the iconaddy variable, you will have to make one small change when doing that though which is to add the async setting in your ajax request and set the value to false which will make the request synchronous which means the code following the ajax request will only get triggered upon the request been completed first.

See the below example if that didn’t make sense.

var myVariable = '';

$.ajax({
    async   : false,
    success : function(data) {
        myVariable = data;
    }
});

Thanks for your response chris. I have adjusted my code to match was you suggested, but it appears I’m still having issues. Here’s my code and I have attached a screenshot of the response. The blue line represents a particular line from the response I’m after.


$(document).ready(function(){

	$("a[href*='www.wowhead.com/spell'] , a[href*='www.wowhead.com/achievement']").each(function(){
		
		var wowheadlink = $(this).attr('href') + '&power';
		
		var iconaddy = '';
		
		$.ajax({
			async: false,
			url: wowheadlink,
			type: 'get',
			dataType: 'jsonp',
			async   : false,
			success : function(data) {
			
				iconaddy = data;
			  
				console.log(iconaddy);
				
			}
			
		});
		
	});	
	
});

Thanks again.

In order to access that specific key within the object you would need to remove the code around it so the response reads something like:

({
    icon : 'name_here'
})

Apart from that there is no simple way to extract the data from the response you posted above.