Rya*_*nal 33
在这里,你很简单:
function createXHR()
{
var xhr;
if (window.ActiveXObject)
{
try
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert(e.message);
xhr = null;
}
}
else
{
xhr = new XMLHttpRequest();
}
return xhr;
}
Run Code Online (Sandbox Code Playgroud)
文档在这里
例:
var xhr = createXHR();
xhr.onreadystatechange = function()
{
if (xhr.readyState === 4)
{
alert(xhr.responseText);
}
}
xhr.open('GET', 'test.txt', true)
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send()
Run Code Online (Sandbox Code Playgroud)
更新:
要进行跨域脚本编写,您必须调用本地服务器端代理(读取和回显远程数据),或者,如果远程服务返回JSON,则使用此方法:
var s = document.createElement('script')
s.src = 'remotewebservice.json';
document.body.appendChild(s);
Run Code Online (Sandbox Code Playgroud)
由于JSON本质上是一个JavaScript对象或数组,因此这是一个有效的源.理论上你应该能够直接调用远程服务.我没有测试过这个,但它似乎是一个公认的做法:
msa*_*nce 20
您可以构建自己的jQuery版本,只包含AJAX模块.
https://github.com/jquery/jquery#how-to-build-your-own-jquery
https://github.com/jquery/jquery#modules
| 归档时间: |
|
| 查看次数: |
31478 次 |
| 最近记录: |