PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : javascript:



abcdeef
26-04-2006, 17:36
hi,
nutze XMLHttpRequest, um daten dynamisch nachzuladen; dies funktioniert mit konqueror aber nicht mit firefox. er führt die funktion, die die eingehenden daten verarbeitet nicht aus.


var strData="blabla";
var xmlHttp=false;
var strSource="blabla";

if( xmlHttp && xmlHttp.readyState ) {
xmlHttp.abort( );
xmlHttp = false;
}
if(!xmlHttp) {
if (window.XMLHttpRequest) xmlHttp= new XMLHttpRequest();
else if (window.ActiveXObject) xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}



xmlHttp.open( "POST", strSource,false);
xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
xmlHttp.setRequestHeader( 'Content-length', strData.length );
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState==4) {
if( xmlHttp.status == 200) {
var tmp=xmlHttp.responseText;
....

}
}
}
xmlHttp.send(strData);

nEox
27-04-2006, 20:06
Hallo abcdeef,

also wenn ich den Code im IE teste kommen noch andere Fehler (z. B. wird das xmlHttp-Objekt nicht gefunden).

Unter folgendem Link findest du eine ganz gute Erkärung zum Thema:
http://jibbering.com/2002/4/httprequest.html

Gibt es eigentlich "blabla" (strSource="blabla";)?
Weil sonst kann das schlecht funktionieren wenn du nur auf den Status 200 prüfst.

Grüße,

nEox

nul
10-05-2006, 02:37
Ich fang zwar gerade erst mit AJAX an, aber versuch mal die zwei folgenden Funktionen zu verwenden:

var xmlHttp = false;

/** AJAX functions **/

// constants
var REQUEST_GET = 0;
var REQUEST_POST = 2;
var REQUEST_HEAD = 1;
var REQUEST_XML = 3;

/**
* instantiates a new xmlhttprequest object
*
* @return xmlhttprequest object or false
*/
function getXMLRequester() {
var xmlHttp = false;

// try to create a new instance of the xmlhttprequest object
try {
// Internet Explorer
if ( window.ActiveXObject ) {
for( var i = 5; i; i-- ) {
try {
// loading of a newer version of msxml dll (msxml3 - msxml5) failed
// use fallback solution
// old style msxml version independent, deprecated
if( i == 2 )
xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
else // try to use the latest msxml dll
xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
break;
}
catch( excNotLoadable ) {
xmlHttp = false;
}
}
} else if( window.XMLHttpRequest ) { // Mozilla, Opera und Safari
xmlHttp = new XMLHttpRequest();
}
} catch( excNotLoadable ) { // loading of xmlhttp object failed
xmlHttp = false;
}
return xmlHttp;
}

/**
* sends a http request to server
*
* @param strSource, String, datasource on server, e.g. data.php
* @param strData, String, data to send to server, optionally
* @param intType, Integer,request type, possible values: REQUEST_GET, REQUEST_POST, REQUEST_XML, REQUEST_HEAD default REQUEST_GET
* @param strData, Integer, ID of this request, will be given to registered event handler onreadystatechange', optionally
*
* @return String, request data or data source
*/
function sendRequest( strSource, strData, intType, intID ) {
if( !strData )
strData = '';

// default type (0 = GET, 1 = xml, 2 = POST )
if( isNaN( intType ) )
intType = 0; // GET

// previous request not finished yet, abort it before sending a new request
if( xmlHttp && xmlHttp.readyState ) {
xmlHttp.abort( );
xmlHttp = false;
}

// create a new instance of xmlhttprequest object
// if it fails, return
if( !xmlHttp ) {
xmlHttp = getXMLRequester( );
if( !xmlHttp )
return;
}

// parse query string
if( intType != 1 && ( strData && strData.substr( 0, 1 ) == '&' || strData.substr( 0, 1 ) == '?' ) )
strData = strData.substring( 1, strData.length );

// data to send using POST
var dataReturn = strData ? strData : strSource;
switch( intType ) {
case 1: // xml
strData = "xml=" + strData;
case 2: // POST
// open the connection
xmlHttp.open( "POST", strSource, true );
xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
xmlHttp.setRequestHeader( 'Content-length', strData.length );
break;
case 3: // HEAD
// open the connection
xmlHttp.open( "HEAD", strSource, true );
strData = null;
break;
default: // GET
// open the connection
var strDataFile = strSource + (strData ? '?' + strData : '' );
xmlHttp.open( "GET", strDataFile, true );
strData = null;
}

// set onload data event-handler
xmlHttp.onreadystatechange = new Function( "", "processResponse(" + intID + ")" );
// send request to server
xmlHttp.send( strData ); // param = POST data

return dataReturn;
}

nEox
11-05-2006, 22:14
Hi nul,

da fehlt noch die Funktion "processResponse". Die wird hier aufgerufen:

// set onload data event-handler
xmlHttp.onreadystatechange = new Function( "", "processResponse(" + intID + ")" );

Aber vom Prinzip her sehe ich da keinen Fehler.

Grüße,
nEox