如何在JavaScript中HTTP请求URL
How to HTTP Request a URL in JavaScript?
在下面的代码中,我需要执行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>
如何制作?
使用JQuery,您可以执行以下操作:
$.get('http://someurl.com',function(data,status) {
...parse the data...
},'html');
有关更多信息,请查看此问题:请从javascript调用url。
没有Jquery,你可以这样做:
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;
相关文章:
- Meteor如何接收HTTP请求
- 在我的情况下,如何进行http请求
- 使用插件收听Firefox标签的http请求
- http请求使用html而不是json进行响应
- 在node-js路由中发出http请求
- 是否可以在没有HTTP请求或<输入>
- Acync JS HTTP请求通知请求
- PhantomJs在控制台中发出http请求时不会返回任何状态
- PHP处理来自一个页面的http请求,并将其显示在另一个页面上
- Firefox插件-如何发出http请求
- 验收测试主要针对传出的http请求
- 验证HTTP请求来自NFC标签上的点击
- 如何制作'http请求'在Node js中同步
- 为什么我在Cloud9中的http请求总是返回错误
- AngularJS Typeahead和$http请求限制
- HTTP请求使用cURL,但在浏览器中使用javascript时会得到CORS响应
- 如何使用Chrome扩展API获取网页的所有HTTP请求
- 将http请求中的数据传递给C#控制器
- 在初始化路由之前发送$http请求
- 从客户端捕获 HTTP 请求(包括 ajax)