2012-11-22 74 views
0

我正在使用Spring3,struts 2和jquery jquery-1.8.2.js開發web項目。struts 2和jquery ajax調用

這是我的jquery ajax調用

function(){ 

    var data = {}; 

    data['patientFETO.title'] = $('#idSelTitle').val().trim(); 
    data['patientFETO.firstName'] = $('#idFirstName').val().trim(); 
    data['patientFETO.lastName'] = $('#idLastName').val().trim(); 
    data['patientFETO.mobileNumber'] = $('#idMobileNumber').val().trim(); 
    data['patientFETO.idNumber'] = $('#idIDNumber').val().trim(); 
    data['patientFETO.gender'] = $('#idSelGender').val().trim(); 
    data['patientFETO.age'] = $('#idAge').val().trim(); 
    data['patientFETO.dob'] = $('#idDOB').val().trim(); 

    $.ajax({url:'savePatientAction', 
     cache: false, 
     type:"POST", 
     data:data, 
     dataType: 'json', 
     error: function(XMLHttpRequest, textStatus, errorThrown){ 
      alert('Error ' + textStatus); 
      alert(errorThrown); 
      alert(XMLHttpRequest.responseText); 
     }, 
     success: function(data){   
      alert('SUCCESS'); 

      } 

,這是我的支柱動作映射

<action name="savePatientAction" class="appointmentAction" method="doPatientSave"> 
     <result name="success">/account/confirmation.jsp</result> 
     <exception-mapping result="success" exception="e"></exception-mapping> 
    </action> 

當我運行它給 語法錯誤:JSON.parse:意外的字符 「http://localhost:8080/ML/resources/js/jquery-1.8.2.js」 7764行

請給我想法解決此問題 謝謝

回答

0

遵循以下步驟:1。 struts.xml中 - 你需要啓用JSON結果類型 2.在你的動作映射,

做了這些改變後,當你調用響應將是JSON類型,Ajax調用可以處理

+0

你的意思<動作名稱= 「savePatientAction」 類= 「appointmentAction」 方法= 「doPatientSave」> <結果名稱= 「成功」 類型= 「JSON」> /帳戶/確認.jsp

+0

我已經將此部分添加到struts.xml文件中

+0

當結果類型爲json時,不需要JSP - 只需將其留空即可 – saasthasoft

0

功能$.ajax()應該這樣做:error: function(jqXHR, textStatus, errorThrown),不error: function(XMLHttpRequest, textStatus, errorThrown),關於該DOCUMENT

A function to be called if the request fails. 
The function receives three arguments: 
The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, 
a string describing the type of error that occurred 
and an optional exception object, if one occurred. 

由於使用jquery 1.8, you should change the name of the parameter