㈠ 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就可以了.