在分配XMLHttpRequest.responseText时,如何停止获取“未定义”。 [英] How do I stop getting 'undefined' when assigning XMLHttpRequest.responseText.
问题描述
我正在尝试使用ajax更改输入字段中的值,但是当我这样做时,我得到值'undefined'。当我执行document.write(ajax.responseText)时,responseText接缝很好;打印正确的值。我正在使用此页面代码来创建ajax对象(工作正常)http://www.cristalab.com/tutoriales/introduccion-a-ajax-con-php-y-formularios-c165l/。
它接缝我没有以正确的方式分配价值。但我不知道怎么做。我使用了歌剧和铬,但我得到了相同的结果。 proveedor是输入字段的id。我使用的代码是:
function cargar(){
var t2,contenedor;
contenedor = document .forms [ 0 ]。producto.value;
ajax = nuevoAjax();
ajax.open( POST, datos.php, true );
ajax.onreadystatechange = function (){
if (ajax.readyState = = 4 ){
document .getElementById( proveedor)。value = ajax.responeText;
}
}
ajax.setRequestHeader( Content-Type, application / x-www-form-urlencoded);
ajax.send( t2 = + contenedor);
}
和php:
<?php
$ cont = $ _ POST [ t2];
echo $ cont;
?>
cont =
_POST [ t2];
echo
cont;
?>
Hi, I'm trying to change the value in a input field using ajax, but when I do it i get the value 'undefined'. The responseText seams fine, when I do document.write(ajax.responseText); the correct value prints. I'm using this page code to create the ajax object ( works fine) http://www.cristalab.com/tutoriales/introduccion-a-ajax-con-php-y-formularios-c165l/.
It seams that I'm not assinging the value in the right way. but I don't know how to it.I used opera and chrome, but I got the same results. "proveedor" is the id of the input field. The code I used is:
function cargar(){
var t2, contenedor;
contenedor = document.forms[0].producto.value;
ajax=nuevoAjax();
ajax.open("POST", "datos.php",true);
ajax.onreadystatechange=function() {
if (ajax.readyState==4) {
document.getElementById("proveedor").value=ajax.responeText;
}
}
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajax.send("t2="+contenedor);
}
And the php:
<?php
$cont=$_POST["t2"];
echo $cont;
?>
cont=
_POST["t2"]; echo
cont; ?>
这篇关于在分配XMLHttpRequest.responseText时,如何停止获取“未定义”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!