Document.ajax is null avec methode post
chantalgoret
-
chantalgoret -
chantalgoret -
Bonjour,
j ai un soucis d'erreur sur ma page, bien que mon script qui actualise
une page html coté serveur fonctionne...
les erreurs :
"document.ajax is null" pour document.ajax.dyn="Received:" + xhr.responseText;
et "unspecified error" pour xhr.send(null);
voici les scripts simplifiés :
test.js
---------
var url2;
var content2;
function envoyer()
{
content2="hello world";
url2="updated.html";
submitForm();
}
function submitForm()
{
var xhr;
try { xhr = new ActiveXObject('Msxml2.XMLHTTP'); }
catch (e)
{
try { xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
catch (e2)
{
try { xhr = new XMLHttpRequest(); }
catch (e3) { xhr = false; }
}
}
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4)
{
if(xhr.status == 200)
document.ajax.dyn="Received:" + xhr.responseText; //"document.ajax is null"
else
document.ajax.dyn="Error code " + xhr.status;
}
}
var data = "url2=" + url2 + "&content2=" + content2;
alert(data);
xhr.open("POST", "ajax-post.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
xhr.send(null); // "unspecified error"
return;
}
testupdate.html
---------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="JavaScript1.2"></script>
<script type="text/javascript" src="test.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MJ mise a jour</title>
</head>
<body >
<INPUT TYPE="submit" onclick="envoyer();"/>
</body>
</html>
ajax-update
----------------
<?php
$content = $_POST["content2"] ;
$url = $_POST["url2"] ;
$nfile = fopen($url, "w");
fwrite($nfile, $content);
fclose($nfile);
?>
updated.html
-----------------
hello world !
Merci d'avance, ça serait bien urbain si vous pouviez m'aider
j ai un soucis d'erreur sur ma page, bien que mon script qui actualise
une page html coté serveur fonctionne...
les erreurs :
"document.ajax is null" pour document.ajax.dyn="Received:" + xhr.responseText;
et "unspecified error" pour xhr.send(null);
voici les scripts simplifiés :
test.js
---------
var url2;
var content2;
function envoyer()
{
content2="hello world";
url2="updated.html";
submitForm();
}
function submitForm()
{
var xhr;
try { xhr = new ActiveXObject('Msxml2.XMLHTTP'); }
catch (e)
{
try { xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
catch (e2)
{
try { xhr = new XMLHttpRequest(); }
catch (e3) { xhr = false; }
}
}
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4)
{
if(xhr.status == 200)
document.ajax.dyn="Received:" + xhr.responseText; //"document.ajax is null"
else
document.ajax.dyn="Error code " + xhr.status;
}
}
var data = "url2=" + url2 + "&content2=" + content2;
alert(data);
xhr.open("POST", "ajax-post.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
xhr.send(null); // "unspecified error"
return;
}
testupdate.html
---------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="JavaScript1.2"></script>
<script type="text/javascript" src="test.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MJ mise a jour</title>
</head>
<body >
<INPUT TYPE="submit" onclick="envoyer();"/>
</body>
</html>
ajax-update
----------------
<?php
$content = $_POST["content2"] ;
$url = $_POST["url2"] ;
$nfile = fopen($url, "w");
fwrite($nfile, $content);
fclose($nfile);
?>
updated.html
-----------------
hello world !
Merci d'avance, ça serait bien urbain si vous pouviez m'aider
A voir également:
- Document.ajax is null avec methode post
- What is my movie français - Télécharger - Divers TV & Vidéo
- Who is on my wifi - Télécharger - Outils Internet
- Brouillon post instagram - Guide
- Post it windows - Télécharger - Agendas & Calendriers
- Code post - Télécharger - Vie quotidienne
1 réponse
voila j'ai trouvé un moyen de ne plus avoir d'erreurs en enlevant xhr.send(null); qui je pense n'est necessaire qu'avec un GET
j ai aussi modifié la gestion d erreur ==4 == 200 en ne mettant qu'un alert (xhr.status); dans le else
donc mon test.js donne:
----------------------------------
var url2;
var content2;
function envoyer()
{
content2="hello world";
url2="updated.html";
submitForm();
}
function submitForm()
{
var xhr;
try { xhr = new ActiveXObject('Msxml2.XMLHTTP'); }
catch (e)
{
try { xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
catch (e2)
{
try { xhr = new XMLHttpRequest(); }
catch (e3) { xhr = false; }
}
}
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4)
{
if(xhr.status == 200)
var ok="ok";
// document.ajax.dyn="Received:" + xhr.responseText; //"document.ajax is null"
else
alert("error code "+xhr.status);
// document.ajax.dyn="Error code " + xhr.status;
}
}
var data = "url2=" + url2 + "&content2=" + content2;
xhr.open("POST", "ajax-post.php?", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
// xhr.send(null); // "unspecified error"
return;
}
Ca a l'air de bien marcher en local, rese a voir sur un serveur...
Est-ce que mon code est foireux ou pas ?
j ai aussi modifié la gestion d erreur ==4 == 200 en ne mettant qu'un alert (xhr.status); dans le else
donc mon test.js donne:
----------------------------------
var url2;
var content2;
function envoyer()
{
content2="hello world";
url2="updated.html";
submitForm();
}
function submitForm()
{
var xhr;
try { xhr = new ActiveXObject('Msxml2.XMLHTTP'); }
catch (e)
{
try { xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
catch (e2)
{
try { xhr = new XMLHttpRequest(); }
catch (e3) { xhr = false; }
}
}
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4)
{
if(xhr.status == 200)
var ok="ok";
// document.ajax.dyn="Received:" + xhr.responseText; //"document.ajax is null"
else
alert("error code "+xhr.status);
// document.ajax.dyn="Error code " + xhr.status;
}
}
var data = "url2=" + url2 + "&content2=" + content2;
xhr.open("POST", "ajax-post.php?", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
// xhr.send(null); // "unspecified error"
return;
}
Ca a l'air de bien marcher en local, rese a voir sur un serveur...
Est-ce que mon code est foireux ou pas ?