2016-09-25 117 views
0

我想發送一個位圖和一些字符串字段到服務器,我已經徹底檢查沒有參數是空的。這是我的Ajax代碼ajax發送空值到servlet

var URL = "ProfileUpdater"; 

var username = $("#cp-setup-username").val(); 
var email = $("#cp-setup-email").val(); 
var birthday = $("#cp-setup-birthday").val(); 
var aboutYou = $("#cp-setup-about-you").val();  
var intrests = $("#cp-setup-intrests").val(); 
var phoneNumber = $("#cp-setup-phone-number").val(); 
var sessionid = $.cookie('WhitePages_SessionID'); 
var session_username = $.cookie('WhitePages_Username'); 


var legal = emptyCheck(username,email,birthday,aboutYou,intrests,phoneNumber); 
alert(username +" " + email + " " + birthday +" " + aboutYou + " " +intrests + " " + phoneNumber +" "+ sessionid +" " + session_username); 

alert(result); 

if(legal){ 
    alert(sessionid + " " + session_username) ;  
    alert("about call server"); 
    $.ajax({ 
     type:'POST', 
     url : URL, 
     processData:'false', 
     contentType:'false', 

     data:{Username:username,Email:email,Birthday:birthday,AboutYou:aboutYou,Intrests:intrests,PhoneNumber:phoneNumber,sessionid:sessionid,username:session_username,Image:result,FieldsChanged:fieldsChanged,isMultiPart:isMultiPart}, 


     success:function(data){ 
      alert("success"); 
      alert(data); 
     } 
}); 

} 

,這是我的servlet代碼:

import java.io.IOException; 
import java.io.PrintWriter; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
public class ProfileUpdater extends HttpServlet { 



    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 


      String user = request.getParameter("Username"); 
       String sessionid = request.getParameter("sessionid"); 
       String username = request.getParameter("username"); 

       //sessionid and username is null 

       PrintWriter out = response.getWriter(); 
       out.println(sessionid +" " + username + " " + user); 
       System.out.println("hello " + username + "your session id is " +sessionid + "  " + user); 
    } 



} 

我已經把警報在我的劇本,他們是不是空的,但在響應它爲空。我的意思是我認爲它發送空服務器。有人可以告訴我我要去哪裏嗎?謝謝。

+0

檢查瀏覽器啓動工具是否發送了請求參數? –

回答

0

您不能在多部分文章中使用getParameter()。它只是像你發現的那樣返回null。根據您編碼的servlet規範,您可以使用@MultipartConfig註釋(spec 3.0+)或Apache Commons FileUpload庫。註釋方法是內置的,但需要更多的工作; FileUpload庫更容易使用IMO,但顯然需要包含另一個庫。我不確定哪個在資源或速度方面更有效率。有趣的是,如果你使用Tomcat,它使用FileUpload來處理背景中的東西。