www.webdeveloper.com
Results 1 to 7 of 7

Thread: jQuery - get xml data function returns undefined

  1. #1
    Join Date
    Feb 2007
    Posts
    66

    jQuery - get xml data function returns undefined

    Hi, I am trying to make a function that will return part of some xml data, but It keeps returning "Undefined"

    here is the function, with the call:
    Code:
    function getUserInfo(u, t) {
    	$.ajax({
    		type: 'GET',
    		url: 'http://connect.okapinetwork.co.uk/xml/' + u,
    		dataType: 'xml',
    		success: function(data) {
    			return $('user ' + t, data).first().text();
    		}
    	});
    }
    
    getUserInfo('4902030187736', 'name');
    Last edited by vinny619; 11-22-2011 at 08:14 AM.

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Code:
    url: 'http://connect.okapinetwork.co.uk/xml/'
    AJAX alone can not perform requests from a domain to another domain. You need to intermediate the request via a proxy server-side application.

    Even if, theoretically, both documents belong to the same domain, some servers (like Apache) under some configurations, are instructed to block any AJAX request made to an absolute written URL (with the protocol HTTP explicitly written). In this case you must use relative addresses.
    Last edited by Kor; 11-22-2011 at 08:29 AM.

  3. #3
    Join Date
    Feb 2007
    Posts
    66
    thats odd as it works if i use:
    Code:
    success: function(data) {
    	$("#test").html( $('user ' + t, data).first().text() );
    }
    but I don't want to write it to an element, I want to write it to a variable within an each loop.
    I.E.
    Code:
    $("oUser").each(function() {
    	var mainElm = $(this);
    	var user = $(this).attr("user");
    	var name = getUserInfo(user, 'name');
    	if( mainElm.attr("type") == 'button' ) {
    		mainElm.html("<button>" + name + "</button>" );
    	}else{
    		mainElm.html("" + name );
    	}
    });
    
    <oUser type="button" user="4902030187736"></oUser>

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    It might have worked only in IE and only if the file was running locally. When you rise your site on a server, it won't work, I assure you.

  5. #5
    Join Date
    Feb 2007
    Posts
    66
    so how does facebook and google do it then for their "likes" and "+1/circles" buttons...

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by vinny619 View Post
    so how does facebook and google do it then for their "likes" and "+1/circles" buttons...
    They use either an iframe (loaded by you in your page) to nest that "like" button (and that iframe loads a document from the same domain as Facebook) or a JavaScript SDK, which allows you to load a javascript code placed on their domain. That means, in both cases, the javascript code runs within the borders of the same domain.

  7. #7
    Join Date
    Feb 2007
    Posts
    66
    damn, okay thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles