简体   繁体   English

无法获得ajax响应

[英]unable to get the ajax response

I am new to symfony. 我是symfony的新手。 I have created a twig file named as index.html.twig . 我创建了一个名为index.html.twig的树枝文件。 In this I have written a code for making an ajax request as follow: 在此,我编写了一个代码来发出ajax请求,如下所示:

<script language="javascript">
    function onsub()
    {
        alert(document.getElementById('source').value);
        var http=new XMLHttpRequest();
        var name="rohit";
        if(http.open("POST", "sub.html.php?field="+name, true)) alert('yes');
        alert(http);
        http.onreadystatechange = function() 
        {
            if(http.readyState == 4 && http.status == 200) 
            {

                alert('i m back');
            }
            else
            {
                alert('sorry');
            }
        }  

        http.send();
    }

    </script>

This is the action called when I press the submit button to submit a form. 这是我按下“提交”按钮提交表单时调用的操作。 when I submit the form, this action is called and the correct value is poped out but it is not sending the data to requested file ie sub.html.php . 当我提交表单时,将调用此操作并弹出正确的值,但它不会将数据发送到请求的文件,即sub.html.php it is making the object correctly. 它使物体正确。 and when I am printing the value of http.readyState then it is giving the answer as 1 . 当我打印http.readyState的值时,它给出的答案是1 so I want to send the value of name to the requested file sub.html.php . 所以我想将name的值发送到请求的文件sub.html.php what can I do for this? 我该怎么办?

try this ..... 尝试这个 .....

<script language="javascript">
function onsub() 
{ 
   var name="rohit";
   alert(document.getElementById('source').value); 
   if(window.XMLHttpRequest)
   { 
      http=new XMLHttpRequest();
   } 
   else 
   { 
    // code    for IE6, IE5 
    http=new ActiveXObject("Microsoft.XMLHTTP"); 
   }
   http.onreadystatechange=function() 
   { 
     if(http.readyState == 4 &&    http.status == 200) 
     { 
       alert('i m back'); 
     } 
     else 
     { 
       alert('sorry'); 
     }
   }    
   http.open("GET", "sub.php?field="+name , true); 
   http.send(); 
   }    
   </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM