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?

5
  • 2
    by "execute" you mean to send a HTTP request? Commented Dec 12, 2015 at 17:35
  • @Sulthan yes it's is a HTTP request Commented Dec 12, 2015 at 17:37
  • You need AJAX request from javascript blog.garstasio.com/you-dont-need-jquery/ajax Commented Dec 12, 2015 at 17:37
  • @YuriyTumakha nice blog, would you mind if I updated my answer with it? :) Commented Dec 12, 2015 at 17:42
  • 1
    @gsamaras +1 for me. and you can reuse it:) Commented Dec 12, 2015 at 17:46

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

gsamaras, I edited the question for details... and is this (without Jquery) an asynchronous request?
@rozero, the answer lies here: developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest Hope that helps.
The problem was with seamonkey browser. With the firefox it works. But why whe the page jumps up?
@rozero because we are telling it to do so! ;)
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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.