0

我正在使用 Spring3、struts 2 和 jquery jquery-1.8.2.js 开发 web 项目。

这是我的 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');

           }

这是我的 struts 动作映射

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

当我运行它给出 SyntaxError: JSON.parse: unexpected character " http://localhost:8080/ML/resources/js/jquery-1.8.2.js " Line 7764

请给我解决这个问题的想法谢谢

4

2 回答 2

0

请按照以下步骤操作: 1. struts.xml - 您需要启用 JSON 结果类型 2. 在您的操作映射中,

进行这些更改后,当您调用响应将是 JSON 类型,ajax 调用可以处理

于 2012-11-22T05:59:40.570 回答
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

于 2012-11-22T15:58:04.927 回答