如何在JavaScript中通过HTTP请求URL?

如何在JavaScript中通过HTTP请求URL?,javascript,jquery,Javascript,Jquery,在下面的代码中,我需要执行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

在下面的代码中,我需要执行
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>

函数callMyAction(){
var newUrl='/makegovered.action';
//在这里,我需要执行newUrl,即称之为remote
//它将不返回任何内容。它只是启动服务器进程
}

如何制作?

使用JQuery,您可以执行以下操作:

$.get('http://someurl.com',function(data,status) {
      ...parse the data...
},'html');
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();
有关更多信息,请检查此问题:请


没有Jquery,您可以执行以下操作:

$.get('http://someurl.com',function(data,status) {
      ...parse the data...
},'html');
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();

如本文所述,Yuriy Tumakha善意地建议。

前面的回答建议使用JQuery代码。如果您不熟悉JQuery,下面是纯JavaScript代码。但建议使用一些框架,如JQuery,以便于更好地管理代码

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;

“执行”是指发送HTTP请求吗?@Sulthan是的,这是一个HTTP请求。您需要javascript@YuriyTumakha nice blog的AJAX请求,您介意我用它更新我的答案吗?:)@gsamaras+1对我来说。你可以重复使用:)gsamaras,我编辑了问题的细节。。。这(没有Jquery)是一个异步请求吗?@rozero,答案就在这里:希望有帮助。问题是seamonkey浏览器。在firefox上,它可以工作。但是为什么页面会跳起来?@rozero,因为我们告诉它这样做!;)