2

In the below code I need to execute the newUrl:

<SCRIPT type="text/javascript">
    function callMyAction(){
    var newUrl = '/makeObtained.action';
    //here I need to execute the newUrl i.e. call it remote
    //it will return nothing. it just starts server process
    }
</SCRIPT>

<a onclick="callMyAction()" href='...'> ...</a>

How to make it?

rozero
  • 149
  • 3
  • 15

2 Answers2

4

With JQuery, you can do this:

$.get('http://someurl.com',function(data,status) {
      ...parse the data...
},'html');

For more info, check this question: Call a url from javascript please.


Without Jquery, you can do this:

var xhr = new XMLHttpRequest();
xhr.open('GET', encodeURI('myservice/username?id=some-unique-id'));
xhr.onload = function() {
    if (xhr.status === 200) {
        alert('User\'s name is ' + xhr.responseText);
    }
    else {
        alert('Request failed.  Returned status of ' + xhr.status);
    }
};
xhr.send();

as described in this blog, kindly suggested by Yuriy Tumakha.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

As previous answer suggest JQuery code. Here is the code with pure JavaScript if you are not familiar with JQuery. But it is recommended to use some framework like JQuery for easy and better code management.

var xmlhttp;

if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
}

else{ // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

//Replace ajax_info.txt with your URL.
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();

xmlDoc=xmlhttp.responseXML;
Partha Sarathi Ghosh
  • 10,936
  • 20
  • 59
  • 84