duanjianshen4871
2017-07-05 14:19
采纳率: 100%
浏览 342

http.responseText为空

I have a problem with my http.responseText (is always empty). I post my code:

function bCheckName ()
{
// It checks if the browser can allow a http request 
if ( window.XMLHttpRequest ) 
{
    xhttp = new XMLHttpRequest();
} 
else 
{
    // for IE6, IE5
    xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

// It takes the name from the form
var firstName = document.getElementById("firstName").value;
var datastring = "firstName=" + firstName;
var datastring_escaped = encodeURIComponent ( datastring );

// It opens the request to thye server
xhttp.open ( "POST", "../form/formValidation.php", true );

// It sets the header
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

// It sends the data to the server
xhttp.send( datastring_escaped );

// It takes the responde from the server
xhttp.onreadystatechange = function() 
{
    if ( xhttp.readyState == 4 && xhttp.status == 200 ) 
    {

        var string      = xhttp.responseText.substr ( 0, 2 );
        var response    = xhttp.responseText.substr ( 5 );

        if ( string == "OK")
        {
            document.getElementById("nameResponse").innerHTML           = '<img src = "../img/pages/contact/true.png" alt = "correct answer" >';
            document.getElementById("response").innerHTML               = response;
        }
        else
        {
            document.getElementById("nameResponse").innerHTML           = '<img src = "../img/pages/contact/error.png" alt = "wrong answer">';
            document.getElementById("response").innerHTML               = response; 
        }
    }
}

return false;}

If I replace "xhttp.send( datastring_escaped );" with "xhttp.send( datastring );", everything will work as expected. How can I fix the problem. I post also the php code:

        if ( isset( $_POST['firstName'] ))
        {

        echo( "OK - ".urldecode ( $_POST['firstName']) );

        }

How can I solve the problem? Thanks in advance!!!

Francesco

图片转代码服务由CSDN问答提供 功能建议

我的http.responseText有问题(总是为空)。 我发布了我的代码:

  function bCheckName()
 {
 //它检查浏览器是否允许http请求
if(window.XMLHttpRequest)
 {  
 xhttp = new XMLHttpRequest(); 
} 
else 
 {
 // for IE6,IE5 
 xhttp = new ActiveXObject(“Microsoft.XMLHTTP”); 
} 
 
 //需要 形式为
var firstName = document.getElementById(“firstName”)。value; 
var datastring =“firstName =”+ firstName; 
var datastring_escaped = encodeURIComponent(datastring); 
 
 //它打开请求 到thye server 
xhttp.open(“POST”,“.. / form / formValidation.php”,true); 
 
 //它设置标头
xhttp.setRequestHeader(“Content-type”,“application /”  x-www-form-urlencoded“); 
 
 //它将数据发送到服务器
xhttp.send(datastring_escaped); 
 
 //它从服务器获取响应
xhttp.onreadystatechange = function  ()
 {
 if(xhttp.readyState == 4&amp;&amp; xhttp.status == 200)
 {
 
 var string = xhttp.responseText.substr(0,2); 
 var  respo  nse = xhttp.responseText.substr(5); 
 
 if(string ==“OK”)
 {
 document.getElementById(“nameResponse”)。innerHTML ='&lt; img src =“../  img / pages / contact / true.png“alt =”正确答案“&gt;'; 
 document.getElementById(”response“)。innerHTML = response; 
} 
 else 
 {
 document.getElementById(  “nameResponse”)。innerHTML ='&lt; img src =“../img/pages/contact/error.png”alt =“错误答案”&gt;'; 
 document.getElementById(“response”)。innerHTML = 响应;  
} 
} 
 
 
返回false;} 
   
 
 

如果我替换“xhttp.send(datastring_escaped);” 使用“xhttp.send(datastring);”,一切都将按预期工作。 我该如何解决这个问题。 我也发布了php代码:

  if(isset($ _POST ['firstName']))
 {
 
 echo(“OK  - ”.urldecode(  $ _POST ['firstName'])); 
 
} 
   
 
 

如何解决问题? 谢谢提前!!!

Francesco

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongwu3596 2017-07-05 14:26
    已采纳

    encodeURIComponent is only used for a part (component) of the URI.

    encodeURIComponent("firstName=foobar")
    

    will give you "firstName%3Dfoobar". There will be no firstName request parameter and you can't read it from $_POST['firstName'].

    If you really need to encode it, then only encode the firstName variable:

    var datastring_escaped = "firstName=" + encodeURIComponent(firstName);
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题