㈠ AJAX技术是什么,和js的区别
ajax只不过就是JS里面的一个分支而已 只不过调用了一个XMLHTTP组件而已下面代码就是一个ajax 你觉得他和JS代码有没有什么区别? 根本没区别function ajax(){ this.method; this.url; this.responsetype; this.content; var http_request = false; this.getExecObj = function(reValue){ if(window.XMLHttpRequest) { http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { http_request.overrideMimeType("text/xml"); } } else if (window.ActiveXObject) { try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!http_request) { window.alert("创建XMLHttpRequest对象实例失败."); return false; } if(this.method.toLowerCase()=="get") { http_request.open(this.method, this.url, true); } else if(this.method.toLowerCase()=="post") { http_request.open(this.method, this.url, true); http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); } else { window.alert("http请求类别参数错误。"); return false; } http_request.send(this.content); var reTextResponse = function() { if (http_request.readyState == 4) { if (http_request.status == 200) { reValue(http_request.responseText); } else { alert("页面有异常。"); } } } var reXMLResponse = function() { if (http_request.readyState == 4) { if (http_request.status == 200) { reValue(http_request.responseXML); } else { alert("页面有异常。"); } } } if(this.responsetype.toLowerCase()=="text") { http_request.onreadystatechange = reTextResponse; } else if(this.responsetype.toLowerCase()=="xml") { http_request.onreadystatechange = reXMLResponse; } else { window.alert("参数错误。"); return false; } } } // 调用方法 var _ajax = new ajax()_ajax.method = "post"; //是get还是post_ajax.url = " http://www.pb4schools.net/Returnlist.aspx"; //请求的地址_ajax.responsetype = "text"; //处理返回内容的类型_ajax.content = "id=2"; //发送的内容_ajax.getExecObj( //对返回值处理 function(str){ document.getElementById("select1").outerHTML = "选择大产品"+ str + "" });
㈡ 怎么用原生JS实现类似jQuery的ajax
建议你这样试试看:
ajax函数:
function ajax(opt) {
opt = opt || {};
opt.method = opt.method.toUpperCase() || 'POST';
opt.url = opt.url || '';
opt.async = opt.async || true;
opt.data = opt.data || null;
opt.success = opt.success || function () { };
var xmlHttp = null;
if (XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
} var params = [];
for (var key in opt.data) {
params.push(key + '=' + opt.data[key]);
}
var postData = params.join('&贺备');
if (opt.method.toUpperCase() === '饥山POST') {
xmlHttp.open(opt.method, opt.url, opt.async);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
xmlHttp.send(postData);
}
else if (opt.method.toUpperCase() === 'GET') {
xmlHttp.open(opt.method, opt.url + '?' + postData, opt.async);
xmlHttp.send(null);
}
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
opt.success(xmlHttp.responseText);
}
};
}
PS:希禅肢毁望本文对你有所帮助~qingwei.tech
㈢ 初学JS 学到AJAX到了一个地方结果不回了 onreadystatechange这里, 下面上代码,帮我看一下,愁死我了。
你盯中少些了一个方法
if(request)
{
request.open("GET","example.txt",true);
request.send();
//这就是你少了的那个方法,send方法代表发大拦送的意凯仿山思,你不写它就不会向后台发送请求,所以我刚刚调试的时间,显示的状态老是1 (代表连接成功等待发送)
我们要的状态是4(request.readyState==4)
request.onreadystatechange = function(){
if(request.readyState==4)
{
var para = document.createElement("p");
var txt = document.createTextNode(request.responseText);
para.appendChild(txt);
document.getElementById("new").appendChild(para);
}
}
}
㈣ javascript中的ajax不能得到return 的值,求教处理的方法
你必须知道的是:ajax是异步嫌神获取数据,在函数中,ajax部分基本上是不会按照代码顺序执行的。
你的代码中,ajax.onreadystatechange中的代码不会再alert中执行,镇虚而是在alert之后执行,所以,你获取御者燃不到返回值。
㈤ 请问各路javascript大侠:Ajax重构中的this.onload.call(this);还有this.onerror.call(this);是什么意思
call方法只能对函数有效。按照上述写法,在上述代码中,应当有这样的语句,
xhr.onload=function(){};
xhr.onerror=function(){};
否则不专合理(this.onload会显示未定义属)。
再看你的问题,this.onload指向的是绑定在此事件上的函数,一旦调用call函数就会立即执行,看上去的确是如此。后面的this.onerror同理。
㈥ ajax返回javascript代码并执行
可以考虑用js的eval()方法,该方法接受一个字符串为参数,并将字符串视为一段版js代码并执行。当然权,出于安全考虑,很多人不建议在代码中使用该函数,要用这个函数,你只能自己去保证ajax返回的代码是安全的。
㈦ javascript ajax 回调函数req.status == 200不执行
req.readyState == 4说明ajax成功的返回了,等于200时候就是要从闷此后蚂没迅台把要打印出来的东西给调回前台,不一定要=200,不同浏察核览器值不一样 你可以这样:
if((req.status >= 200&&req.status<=300) || status==304 )
㈧ 请教,关于AJAX,这里的readystate为什么是4或者是complete啊
这里的状态一般有下面几种:
1、0:请求未初始化(还没有调用 open())。
2、1:请求已经建立,但是还没有发送(还没有调用 send())。
3、2:请求已发送,正在处理中(通常现在可以从响应中获取内容头)。
4、3:请求在处理中;通常响应中已有部分数据可用了,但是服务器还没有完成响应的生成。
5、4:响应已完成;
只有4 和"complete"才可以获取并使用服务器的响应。
(8)jsajaxreadystate扩展阅读:
AJAX请求的五个步骤:
1、创建XMLHttpRequest异步对象。
var xhr;if (window.XMLHttpRequest) {
xhr=new XMLHttpRequest();
}else {
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
2、 设置回调函数
xhr.onreadystatechange = callback
3、使用open方法与服务器建立连接
xhr.open("get", "test.php", true)
xhr.open("post", "test.php", true)
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
4、 向服务器发送数据
xhr.send(null)
xhr.send("name=jay&age=18")
5、 在回调函数中针对不同的响应状态进行处理
function callback() {
if(xhr.readyState == 4) {
if(xhr.status == 200) {
var res = xhr.responseText
res = JSON.parse(res)
}}}
㈨ JS做AJAX请求发送URL的时候PHP _GET不到URL中出现的#、&和+
原因很简单橘含, 在HTTP请求中只要跟了卜首"&"在PHP里就会被认作是一个参数. 比如你这个问题的网络URL
http://..com/question/356912881.html?fr=uc_push&push=core&oldq=1
按照你test1.php?url='+content的写法, 传进PHP的实际上是3个参数:
print_r($_GET):
Array
(
[url] =>型伍数 http://..com/question/356912881.html?fr=uc_push
[push] => core
[oldq] => 1
)
最简单的解决办法是在Javascript中先用encodeURIComponent把URL编码: var content = encodeURIComponent(oConBox.value); 再传进PHP就可以了.