2014-03-03 26 views
-1

我需要讓用戶點擊一個鏈接來顯示一個有窗體的燈箱,提交表單並將用戶重定向到一個新頁面。爲什麼在調用JavaScript函數後頁面會遇到NullPointerException?

爲了實現這一點,我定義了兩個javascript函數,lightboxform函數來顯示lightbox和子窗體函數來提交表單。問題是第二個javascript函數(子表單)將請求發送到後端,而不是重定向到索引頁面,將其參數追加到原始地址。

比方說,我在下面的地址:

www.example.com/show?id=2 

我點擊鏈接以顯示燈箱,提交位於燈箱的形式後,頁面地址得到改變到以下地址:

www.example.com/show?p=3444&q=4555 << id parameter is replaced by p,q which are the parameters that I am trying to submit to backend 

長話短說:我需要讓用戶點擊一個鏈接,顯示一個燈箱,用戶將燈箱中的表單提交到後端並重定向到索引頁面。

代碼

//function to show lightbox 
function lightboxform(){ 
    document.getElementById("lightbox").style.display = "Block"; 
    document.getElementById("cover").style.display = "Block"; 
    if(window.XMLHttpRequest) 
    { 
     xmlhttp = new XMLHttpRequest(); 
    } 
    else 
    { 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() 
    { 
     if(xmlhttp.readyState == 4 && xmlhttp.status == 200) 
     { 
      document.getElementById("lightbox").innerHTML=xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open("get","../../lightbox/myform",false); 
    xmlhttp.send(); 
    return false; 
} 

//function to submit the lightbox form to backend and redirect user to new address 
function subform(){ 
    p = $('#p').val(); 
    q = $('#q').val(); 
    document.getElementById("lightbox").style.display = "Block"; 
    document.getElementById("cover").style.display = "Block"; 
    if(window.XMLHttpRequest) 
    { 
     xmlhttp = new XMLHttpRequest(); 
    } 
    else 
    { 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() 
    { 
     if(xmlhttp.readyState == 4 && xmlhttp.status == 200) 
     {  
      document.getElementById("lightbox").style.display = "none"; 
      document.getElementById("cover").style.display = "none"; 
      window.location = ("http://localhost:8080/index.action"); 
     } 
    } 
    xmlhttp.open("get","../../lightbox/processForm?p="+p+"&q="+q,false); 
    xmlhttp.send(); 
} 

燈箱的形式

<form id="form393" onsubmit="return subform()"> 
     <input id="p" name="p" type="text"/> 
     <input id="q" name="q" type="text"/> 
     <input type="submit" value="submit" /> 
</form> 
+0

向我們展示您的html。也給出確切的錯誤信息。 – Daedalus

+1

另外,你的網址根本不匹配。節目從哪裏來?我看到processForm和myform,但沒有myproducts等。 – Daedalus

+0

問題已更新,我正在編輯html文件以上傳它。 – AlexCartio1

回答

0

嘛,不看怎麼您處理頁面的作品,下面是一個在黑暗中拍攝根據現有的資料;

您的ajax請求的問題是您最終如何執行。在接下來的兩行:

xmlhttp.open("POST","../../lightbox/processForm?p="+p+"&q="+q,false); 
xmlhttp.send(); 

你讓一個POST請求,但不給請求的任何數據。對於POST請求,數據通過標題發送,而不是通過url發送。所以,你必須查詢字符串追加到.send()方法:

xmlhttp.open("POST","../../lightbox/processForm",false); 
xmlhttp.send("p="+p+"&q="+q); 

假設你處理頁面使用接線柱陣列,這應該工作。

編輯:

在問候你的更新,只需簡單window.location不正是正確的,儘管它是沒有錯無論是。根據下面鏈接的文章,雖然窗口對象的Location對象是隻讀的,但它可以分配一個字符串作爲href的一種別名。鑑於這是你目前正在做的,而這不適合你,我建議代替replace。例如:

window.location.replace("http://www.example.com/"); //closest to http redirect 
window.location.href = "http://www.example.com/"; //simulate user clicking link 

我建議你瞭解更多信息請查看this answer,還有MDN article把它。

DEMO

+1

我編輯了代碼,但問題仍然存在。 – AlexCartio1

+1

問題更新 – AlexCartio1

+1

@ AlexCartio1更新。但是,請不要修改您的問題,使這個答案過時,以便它解決的問題不再可見。請添加到它,但不要刪除原來的問題。 – Daedalus

相關問題