NeWorld

Pradžia su ajax

by neworld on Bal.04, 2007, under ajax

Turbūt daugelis žino kas yra ajax, todel atskirai šios temos nenagrinėsiu.

Pats paprasčiausias variantas yra:

Visa šita kodą reikia priskirti pačioje pradžioje, prieš vykdant ajax

[code lang="javascript"]//ši funkcija mum sukurs objektą, kuris yra ajax pagrindas.
function createRequestObject() {
var ajax;
var browser = navigator.appName;
if (browser == “Microsoft Internet Explorer”) {
ajax = new ActiveXObject(“Microsoft.XMLHTTP”);
} else {
ajax = new XMLHttpRequest();
}
return ajax;
}
//musu kintamajam http bus priskirta
var http = createRequestObject();

//mūsų funkcija skirta pačiam paprasčiausiam ajax panaudojimui
function sndReq(adr,handle) {
//ši funkcija nusiunčia GET methotu duomenis adresu adr
http.open('get', adr);
//kai duomenys bus nusiusti ir parsiusti bus vykdima handle kintamajam priskirta funkcija. Visos funckijos turi būti be argumentų, t.y. myhandle() tinka, tačiau myhandle(duomuo, antrasduomuo); nebetinka
http.onreadystatechange = handle;
//si funkcija skirta tik tuo atveju jeigu siunčiami POST metodu duomenys
http.send(null);
}

function myhandle() {
if (http.readyState == 4) {
var response = http.responseText;
alert(response);
}
}

[/code]

na o panaudoti galima šitaip

[code lang="javascript"]sndReq('labas.php',myhandle) [/code]

o labas.php turetu atrodyti maždaug šitaip:

[code lang="php"][/code]

Jei viskas gerai turėtumėte pamatyti lentelę su užrašu “sveikas, kaip sekasi”.

Straipsnis parašytas remiantis pixel.lt

  Balsuoti

Leave a Reply

 

You need to log in to vote

The blog owner requires users to be logged in to be able to vote for this post.

Alternatively, if you do not have an account yet you can create one here.

Powered by Vote It Up

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...