在分配XMLHttpRequest.responseText时,如何停止获取“未定义”。 [英] How do I stop getting 'undefined' when assigning XMLHttpRequest.responseText.

查看:66
本文介绍了在分配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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆