2014-09-26 56 views
1

我想使用Struts動作表單發送ajax文章。 我已經成功使用jQuery創建這種類型的調用。AngularJS AJAX POST Struts1

Struts1的ActionForm的:

public class AjaxForm extends ActionForm { 
    private static final long serialVersionUID = 7403728678369985647L; 

    private String name = null; 
    private FormFile uploadedFile = null; 


    public FormFile getuploadedFile() { 
     return uploadedFile; 
    } 

    public void setFile(FormFile uploadedFile) { 
     this.uploadedFile = uploadedFile; 
    } 


    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
} 

Struts1的行動:

public class AjaxAction extends Action{ 


public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 


    AjaxForm ajaxForm = (AjaxForm)form; 
    System.out.println("Hello " + ajaxForm.getName()); 

jQuery的阿賈克斯郵政工作:

 <script type="text/javascript"> 
     function doAjaxPost() { 
      // get the form values 
      //var name = $('#name').val(); 

      var formData ={ 
        'name':'jQuery_Oron' 
      }; 

      $.ajax({ 
       type: "POST", 
       url: "/BusinessProcess_Project/AjaxSubmit.do", 
       // data: "name=" + 'jQuery', 
       data:formData, 
       dataType: "text/json", 
       success: function(response){    

       }, 
       error: function(e){ 
        alert('Error: ' + e); 
       } 
      }); 
     } 
    </script> 

AngularJS阿賈克斯後notWorking:

 $scope.myShabi = {}; 

     $scope.myShabi.doClick = function(item, event) {  
     // $scope.init = function(item, event) {   
      $scope.loading = true; 


      var formData ={ 
        'name':'Angular_Oron' 
      }; 

      var responsePromise = $http({ 
        method: 'POST', 
        url: '/BusinessProcess_Project/AjaxSubmit.do', 
        data: formData 
      }); 


      responsePromise.success(function(data, status, headers, config) { 
      $scope.jsonFromServer = data; 

       $scope.status ="Finished"; 
       $scope.loading = false; 
      }); 

      responsePromise.error(function(data, status, headers, config) { 
       alert("AJAX failed!"); 
      }); 
     }; 

出於某種原因角不喜歡我送的ActionForm的數據的方式。

請參閱是否可以幫忙。 非常感謝:)

+0

你得到什麼錯誤?順便說一句,我認爲你應該鏈接成功和錯誤,以避免不必要的查找。 'var responsePromise = $ http(...)。success(...)。error(...)' – laruiss 2014-09-26 10:01:47

+0

嗨,感謝您的更新,我沒有收到錯誤,只是得到:'你好null' – 2014-09-26 12:00:23

+0

如果添加'responseType:「json」'會怎麼樣? 變種responsePromise = $ HTTP({ 方法: 'POST', URL: '/BusinessProcess_Project/AjaxSubmit.do', 數據:FORMDATA, 的responseType: 「JSON」 }); – laruiss 2014-09-26 12:11:05

回答

2

這不是data,你應該使用,但params

... 
var responsePromise = $http({ 
    method: 'POST', 
    url: '/BusinessProcess_Project/AjaxSubmit.do', 
    // data: formData 
    params: formData 
}); 
... 
+0

是的!謝謝! – 2014-09-26 14:22:46