Link to home
Start Free TrialLog in
Avatar of ravigosai
ravigosai

asked on

Jquery Get function

Hello All,

I am new to the web development . I am given a task to submit a star rating using j query plug in and then retrieve it back when ever page loads. so I am able to submit the rating to the database. now I need to use j query get function to retrieve the records back and it comes in to XML format. and then I need to show the ratings from there.

 I need to load them into my page once the page loads we need to display this results using J query's Get function. I am having URL which I need to use for Get function and that is going to give me XML string and I need to fetch result out of that.

Please help me with given problem

Thank You in advance.

Avatar of leakim971
leakim971
Flag of Guadeloupe image

Avatar of ravigosai
ravigosai

ASKER

Hello leakim971 I need help also with get function in loading. in that post the guy was able to load and he needed help in reading. Can you please help me with get function... thank you

Give me an example of xml or a live link to see it in action
its not Intranet so cant give you the link for that.. But Here I am providing you the URLs I need to give to get function which will give me xml file automatically.

http://apps.intrastage/ADM/eForms/services/ClassRecordsXML.aspx?class_id=1390&filter=WHERE+foreignid%3D1

and the sample xml file for this XMl look like this.

XML Sample
<document>
  <id></id>
  <class>1390</class>
  <data>
    <string>
      <name>foreignid</name>
      <value>1</value>
    </string>
    <string>
      <name>userid</name>
      <value>2</value>
    </string>
    <integer>
      <name>vote</name>
      <value>3</value>
    </integer>
    <string>
      <name>comment</name>
      <value>4</value>
    </string>
  </data>
</document>

So I will get this XML file like this and  I need to get the value for vote out of it.
Are you OK with jquery ?
jquery is new for me but I will have to use jquery onlly..
Ok let's go for simple JS, no jQuery :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
    window.onload = function() {
        if(window.XMLHttpRequest) xmlhttp = new XMLHttpRequest(); else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        if(xmlhttp) {
            xmlhttp.open("GET", "get.xml", false);
            xmlhttp.send();
			var xml = xmlhttp.responseXML;

			try { var id = xml.getElementsByTagName("id")[0].firstChild.nodeValue } catch(e) { id = "NO ID !!!" };
			alert("id : " + id);

			var class = xml.getElementsByTagName("class")[0].firstChild.nodeValue;
			alert("class : " + class);

			var data = xml.getElementsByTagName("data")[0].getElementsByTagName("string");

			var nbString = data.length;

			alert("first name  : " + data[0].getElementsByTagName("name")[0].firstChild.nodeValue);
			alert("first value  : " + data[0].getElementsByTagName("value")[0].firstChild.nodeValue);

			alert("last name  : " + data[nbString-1].getElementsByTagName("name")[0].firstChild.nodeValue);
			alert("last value  : " + data[nbString-1].getElementsByTagName("value")[0].firstChild.nodeValue);

        }
        else {
            alert("your browser don't support Ajax!\nBye!");
        }
    }
</script>
</head>
<body>
</body>
</html>

Open in new window

Thank for this but I will have to use jquery only.. and also in your example how do we get the xml file form the given URL?
>Thank for this but I will have to use jquery only..
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		$.get("get.xml", function(xml) {
			alert("id : " + $("id", xml).text() );

			alert("class : " + $("class", xml).text() );

			nbString = $("data>string", xml).length;

			alert("first name  : " + $("data>string:eq(0)>name", xml).text() );
			alert("first value  : " + $("data>string:eq(0)>value", xml).text() );

			alert("last name  : " + $("data>string:eq(" + (nbString-1) + ")>name", xml).text() );
			alert("last value  : " + $("data>string:eq(" + (nbString-1) + ")>value", xml).text() );
		});
	});
</script>
</head>
<body>
</body>
</html>

Open in new window


>and also in your example how do we get the xml file form the given URL?
Replace "get.xml" by "http://apps.intrastage/ADM/eForms/services/ClassRecordsXML.aspx?class_id=1390&filter=WHERE+foreignid%3D1"
Thank You very much leakim971. I tried to use it in my code but I dont know why its not giving me any alerts. i am copying my complete Script part please let me know if I need to change any thing.


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>STAR RATING</title>
   
    <script type ="text/javascript" src ="js/jquery.js"></script>  
    <script type="text/javascript" src="js/jquery.raty.js"></script>
    <script type="text/javascript" src="js/jquery.raty.min.js"></script>    
    <script language="javascript">
      $(document).ready(function() {
            $.get("http://apps.intrastage/ADM/eForms/services/ClassRecordsXML.aspx?class_id=1390&filter=WHERE+foreignid%3D1", function(xml) {
                  alert("id : " + $("id", xml).text() );

                  alert("class : " + $("class", xml).text() );

                  nbString = $("data>string", xml).length;

                  alert("first name  : " + $("data>string:eq(0)>name", xml).text() );
                  alert("first value  : " + $("data>string:eq(0)>value", xml).text() );

                  alert("last name  : " + $("data>string:eq(" + (nbString-1) + ")>name", xml).text() );
                  alert("last value  : " + $("data>string:eq(" + (nbString-1) + ")>value", xml).text() );
            });
      });
</script>

   
</head>
<body>
<form id="form1" runat="server">  
       
   
<script type="text/javascript">


$(function() {
      
          $('div.raty-class').each(function() {
              var $this = $(this);
              
      
              $this.raty({
                  start: $this.attr(5),
                  showCancel: true,
                  cancelHint: 'Remove My Rating',
                  onClick: function(score) {
                  
                      $.ajax({
                      type: "POST",
                      url: "Default2.aspx",
                      data: "scr=" + score,
                      success: function(data){
                      alert(score);
                      }
                      });
                      
                  }
              });
          });
      });
      
      

</script>
             
        <div id="1" class="raty-class"></div>
        <div id="2" class="raty-class"></div>
        <div id="3" class="raty-class"></div>
                                 
 </form>
</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried this code also but no Luck no alret boxes...
please confirm your page is on the same domain (protocol and address so : http://apps.intrastage) the the xml file

What happen when you put : http://apps.intrastage/ADM/eForms/services/ClassRecordsXML.aspx?class_id=1390&filter=WHERE+foreignid%3D1
in your web browser ? For example IE ? You get an XML string or you're able to expand the xml nodes ?
Yes I get the XML file open on my webpage when I put that link on webpage... but when we try to use it in the get function its not working..
please confirm your page is on the same domain (protocol and address so : http://apps.intrastage) the the xml file
I am sorry I didnt get you..
What is the full url of your page ?
apps.intradev/mydev/rgosai/
does it make any difference if they are not on same page...
Sorry same domain??
The encoding I think the problem is with encoding. Can you please let me know that in this case what should be the we write if the foreign id is 13.? I think that can solve the problem and we can kind of run it.
Because I was debugging it in Firebug and I think its not taking the URl and its not going inside the function.
>does it make any difference if they are not on same page...
>Sorry same domain??

Yes :
ID:34660594
ID:34663550
Because I am given this URl I am sure it should work. I am just not sure what should I change in URl if suppose the value of foreign id changes... I think that can solve the issue.
Ok I have made sure %3d means = and I have value of the foreign id as 13 and still it is not going in the function. Its directly going to the next function.. Please let me know if there is something wrong about the function...
@leakim971 Yes You are right it is on different domain. I think that is the reason it is not accepting it. So is there a way we can create the proxy of something to make it work on using the same function?
>So is there a way we can create the proxy of something to make it work on using the same function?

Yes : http://www.johnchapman.name/aspnet-proxy-page-cross-domain-requests-from-ajax-and-javascript/
Ok so what change do we need to do in our get function if we use this Proxy page for cross domain relay..
replace :
$.get("http://apps.intrastage/ADM/eForms/services/ClassRecordsXML.aspx?class_id=1390&filter=WHERE+foreignid%3D1", function(xml) {

Open in new window

by :
$.get( "http://apps.intradev/mydev/rgosai/proxy.aspx?u=" + encodeURICompoent( "http://apps.intrastage/ADM/eForms/services/ClassRecordsXML.aspx?class_id=1390&filter=WHERE+foreignid%3D1" , function(xml) {

Open in new window

Thanx for the article leakim971 I think they were the same as what we saw on the external link. and i also voted yes for the article. but Still I have confusion why do we need to put our ULR into encodeURICompoent(). isnt our URL already encoded?. as When I am trying this way its not showing anything on the page now and I am not even able to debug that script.
You previous URL (the XML stream/file) is a parameter for the proxy (proxy.aspx).
So you give the URL to the proxy, it load the file/stream for you and send it back as response to the ajax call. So you stay on the same domain, protocol.
Yes but do we need to write any thing else to make sure that encodeURIcomponent() works. as I am getting error on firebug saying "encodeURICompoent is not defined".

I got the idea of using Proxy file and I think it should work but dont know why it is giving me this error message. lemme send the function to you as I was required to add one closing breacket in it so I need to make sure that I have wrote it correctly (means there is no syntext error).
$.get("http://apps.intradev/mydev/rgosai/proxy.aspx?u=" + encodeURICompoent( "http://apps.intrastage/ADM/eForms/services/ClassRecordsXML.aspx?class_id=1390&filter=WHERE+foreignid%3D1") , function(xml) {
                  alert("id : " + $("id", xml).text() );

                  alert("class : " + $("class", xml).text() );

                  nbString = $("data>string", xml).length;

                  alert("first name  : " + $("data>string:eq(0)>name", xml).text() );
                  alert("first value  : " + $("data>string:eq(0)>value", xml).text() );

                  alert("last name  : " + $("data>string:eq(" + (nbString-1) + ")>name", xml).text() );
                  alert("last value  : " + $("data>string:eq(" + (nbString-1) + ")>value", xml).text() );
            });

Open in new window

<root>
-
<row>
<ROW_NUMBER>1</ROW_NUMBER>
<DOC_ID>hy6</DOC_ID>
<WF_ID/>
<WF_STATUS/>
<COMMENT>3</COMMENT>
<FOREIGNID>13</FOREIGNID>
<USERID>2</USERID>
<VOTE>5</VOTE>
</row>
</root>

This is the xml file which we get when we copy this URl on the browser...
bad fingers... missing "n" :

encodeURIComponent

and this is the error which I get on the firebug..
encodeURICompoent is not defined
(?)()rgosai (line 21)
noConflict(j=function())jquery.js (line 29)
j = function()
inArray()jquery.js (line 37)
[Break On This Error] $.get("http://apps.intrade...RE+foreignid%3D1") , function(xml) {

Open in new window

Yes Thank God I sent u the code.. Other wise I would have never been able to figure out that its spelling mistake. Thanx.
@leakim971: I have last question before I select your comment as answer. suppose I want to use one of the value of the tag of xml and parameter for another function then How can I do that?

Suppose I have function for the star rating and I want to use vote  which we get from this XML as no of stars in that function how can we do that.
Thank You very much for saving me on this... Will always remember you help on this..
you xml url is a string.
you can modify/built it before using it as parameter

for example :

(I hope I understand your question...)
var classid = 1390;
var fkey = 1;
var trueURL = "http://apps.intrastage/ADM/eForms/services/ClassRecordsXML.aspx?class_id=" + classid + "&filter=WHERE+foreignid%3D" + fkey;
$.get("http://apps.intradev/mydev/rgosai/proxy.aspx?u=" + encodeURICompoent( trueURL ) , function(xml) {

Open in new window

No problem, thanks for the points! Have fun in your life!
@leakim971 Hello I am having issue with the browser caching with the Get function I have created a new question for that can you please help me for that?
Yes Thank You though for replying back...