[英]unable to get the ajax response
我是symfony的新手。 我創建了一個名為index.html.twig
的樹枝文件。 在此,我編寫了一個代碼來發出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>
這是我按下“提交”按鈕提交表單時調用的操作。 當我提交表單時,將調用此操作並彈出正確的值,但它不會將數據發送到請求的文件,即sub.html.php
。 它使物體正確。 當我打印http.readyState
的值時,它給出的答案是1
。 所以我想將name
的值發送到請求的文件sub.html.php
。 我該怎么辦?
嘗試這個 .....
<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.