SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Quality not quantity. bo5ton's Avatar
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    654
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code JavaScript:
    $(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.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.
    Code JavaScript:
    var myVariable = '';
     
    $.ajax({
        async   : false,
        success : function(data) {
            myVariable = data;
        }
    });

  3. #3
    Quality not quantity. bo5ton's Avatar
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    654
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris.upjohn View Post
    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.
    Code JavaScript:
    var myVariable = '';
     
    $.ajax({
        async   : false,
        success : function(data) {[ATTACH=CONFIG]61177[/ATTACH]
            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.

    Code JavaScript:
    $(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.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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:

    Code JavaScript:
    ({
        icon : 'name_here'
    })

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •