10 Replies - 474 Views - Last Post: 04 November 2011 - 02:36 PM

#1 maguscrowley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-August 11

Simple ajax call

Posted 03 November 2011 - 09:49 AM

I'm trying to just get an internal application that delivers xml to work with this ajax call but it doesn't seem to work.




<html>
<head>
<script src="jquery-1.6.4.min.js"></script>
</head>
<body>


	
	<div id="result"><div>
	
	<button id="button">Button</button>
	
	<script>
	$(document).ready(function(){
		$.ajax({
			type: "GET",
			url: "url removed",
			dataType: "xml",
			success: function(xml) {
				$(xml).find('service').each(function(){
					var email = $(this).attr('email');
					$('result').append(email);
					});
				});
			}
		});
	});

	</script>
</body>
	
</html>


If you go to the url itself this is what you'd get

  <?xml version="1.0" encoding="UTF-8" ?> 
- <service>
  <email>Jeffrey.Yao@ssa.gov</email> 
  </service>

This post has been edited by stayscrisp: 03 November 2011 - 10:03 AM
Reason for edit:: Removed confidential pin!


Is This A Good Question/Topic? 0
  • +

Replies To: Simple ajax call

#2 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Simple ajax call

Posted 03 November 2011 - 10:18 AM

Hello,

What is happening? What result are you getting? It looks like you have some syntax errors in your call.
	$(document).ready(function(){
		$.ajax({
			type: "GET",
			url: "url removed",
			dataType: "xml",
			success: function(xml) {
				$(xml).find('service').each(function(){
					var email = $(this).attr('email');
					$('result').append(email);
					});
				}); // }); should not be here 
			} // this should be });
		});
	}); // extra }) 




When troubleshooting it is also helpful to code an error handler for your callback. Try this code with the syntax fixed and an error handler added:
$(document).ready(function(){
	$.ajax({
		type: "GET",
		url: "url removed",
		dataType: "xml",
		success: function(xml) {
			$(xml).find('service').each(function(){
				var email = $(this).attr('email');
				$('result').append(email);
			});
		},
		error:function(xhr, text, error){
			alert("Error: " + text);
		}
	});
});



I didn't test that but I think it is ok. Give it a try and let us know what happens :)
Was This Post Helpful? 0
  • +
  • -

#3 maguscrowley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-August 11

Re: Simple ajax call

Posted 03 November 2011 - 12:09 PM

It produces the error
Was This Post Helpful? 0
  • +
  • -

#4 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Simple ajax call

Posted 03 November 2011 - 12:13 PM

What error does it produce? What does it say?
Was This Post Helpful? 0
  • +
  • -

#5 maguscrowley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-August 11

Re: Simple ajax call

Posted 03 November 2011 - 12:33 PM

Simply "Error: error"
Was This Post Helpful? 0
  • +
  • -

#6 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Simple ajax call

Posted 03 November 2011 - 04:18 PM

Try this for the error handler :
error:function(xhr, text, error){
	alert("Error: " + text + " type:" + error);
}



xhr is an XMLHttpRequest object so you can get information from it by accessing xhr.responseText.

If you are not already using it I suggest you get Firebug or something similar to help you diagnose AJAX call problems. You can view AJAX requests through the console and see what is going to and from the server.
Was This Post Helpful? 1
  • +
  • -

#7 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 745
  • View blog
  • Posts: 1,525
  • Joined: 30-January 09

Re: Simple ajax call

Posted 03 November 2011 - 05:31 PM

This is your XML here:
<?xml version="1.0" encoding="UTF-8" ?> 
  <service>
    <email>Jeffrey.Yao@ssa.gov</email> 
  </service>



The thing you have to remember with XML is that the root node cannot be accessed via a call to it's tagname*. Thus, you cannot use .find('service') as a jQuery selector. But, you can use .find('email') as a selector, as the email nodes do not sit at the root level.

Try using this code for your success function instead:
success: function(xml) {
	$(xml).find('email').each(function(){
		var email = $(this).val();
		$('result').append(email);
	});
},



* The root node can be accessed via calls to parents I believe, but there is usually no need to access the root node, and XML should (IMO) be structured in a way that the root node contain no information but simply acts as a holder for it's children.

This post has been edited by e_i_pi: 03 November 2011 - 05:32 PM

Was This Post Helpful? 1
  • +
  • -

#8 maguscrowley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-August 11

Re: Simple ajax call

Posted 04 November 2011 - 07:38 AM

View Poste_i_pi, on 03 November 2011 - 05:31 PM, said:

This is your XML here:
<?xml version="1.0" encoding="UTF-8" ?> 
  <service>
    <email>Jeffrey.Yao@ssa.gov</email> 
  </service>



The thing you have to remember with XML is that the root node cannot be accessed via a call to it's tagname*. Thus, you cannot use .find('service') as a jQuery selector. But, you can use .find('email') as a selector, as the email nodes do not sit at the root level.

Try using this code for your success function instead:
success: function(xml) {
	$(xml).find('email').each(function(){
		var email = $(this).val();
		$('result').append(email);
	});
},



* The root node can be accessed via calls to parents I believe, but there is usually no need to access the root node, and XML should (IMO) be structured in a way that the root node contain no information but simply acts as a holder for it's children.


I get a no transport error.
Was This Post Helpful? 0
  • +
  • -

#9 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Simple ajax call

Posted 04 November 2011 - 07:58 AM

No transport is usually a result of cross-domain ajax requests. Are you trying to make a request to a URL that is not on your domain?

If you are using jQuery ver 1.5+ you can use this line
jQuery.support.cors = true;



before your call and that should force jQuery to allow cross scripting. Here is a link to a related article.
Was This Post Helpful? 0
  • +
  • -

#10 maguscrowley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-August 11

Re: Simple ajax call

Posted 04 November 2011 - 10:10 AM

View PostJstall, on 04 November 2011 - 07:58 AM, said:

No transport is usually a result of cross-domain ajax requests. Are you trying to make a request to a URL that is not on your domain?

If you are using jQuery ver 1.5+ you can use this line
jQuery.support.cors = true;



before your call and that should force jQuery to allow cross scripting. Here is a link to a related article.



Well the good news is that now the success function is executing (I ran some tests) but it's not getting the email.
Was This Post Helpful? 0
  • +
  • -

#11 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 745
  • View blog
  • Posts: 1,525
  • Joined: 30-January 09

Re: Simple ajax call

Posted 04 November 2011 - 02:36 PM

View Postmaguscrowley, on 04 November 2011 - 10:10 AM, said:

Well the good news is that now the success function is executing (I ran some tests) but it's not getting the email.

Doh. My mistake. I haven't used XML in jQuery before, I assumed you retrieved inner values via the .val() function, but you use .text() apparently. The success function should be:
success: function(xml) {
	$(xml).find('email').each(function(){
		var email = $(this).text();
		$('result').append(email);
	});
},


Here's the link to the page I Google'd to get a better idea about it.

This post has been edited by e_i_pi: 04 November 2011 - 02:36 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1