JQuery数据表将JSON传递给Spring控制器,得到错误400 Bad Request

时间:2014-10-10 20:25:29

标签: java javascript ajax spring-mvc

我有一个javascript对象:

function getSearchBO() {
var individual = ($('#individual').attr('checked') == 'checked') ? "true"
        : "false";
var organization = ($('#organization').attr('checked') == 'checked') ? "true"
        : "false"
var enrollmentId = $('#enrollmentId').attr('value').trim();
var medicareId = $('#medicareId').attr('value').trim();
var npi = $('#npi').attr('value').trim();
var contractId = $('#contractId').attr('value').trim();
var firstName = $('#firstName').attr('value').trim();
var lastName = $('#lastName').attr('value').trim();
var ssn = $('#ssn').attr('value').trim();
var dob = $('#dob').attr('value').trim();
var licenseNumber = $('#licenseNumber').attr('value').trim();
var licenseState = $('#licenseState').attr('value').trim();
var specialtyType = $('#specialtyType').attr('value').trim();
var licenseExpirationDate = $('#licenseExpirationDate').attr('value')
        .trim();
var businessName = $('#businessName').attr('value').trim();
var ein = $('#ein').attr('value').trim();
var organizationType = $('#organizationType').attr('value').trim();
var ciy = $('#ciy').attr('value').trim();
var state = $('#state').attr('value').trim();
var zipCode = $('#zipCode').attr('value').trim();
var startDate = $('#startDate').attr('value').trim();
var endDate = $('#endDate').attr('value').trim();

var searchBO = {
    "individual" : individual,
    "organization" : organization,
    "enrollmentId" : enrollmentId,
    "medicareId" : medicareId,
    "npi" : npi,
    "contractId" : contractId,
    "firstName" : firstName,
    "lastName" : lastName,
    "ssn" : ssn,
    "dob" : dob,
    "licenseNumber" : licenseNumber,
    "licenseState" : licenseState,
    "specialtyType" : specialtyType,
    "licenseExpirationDate" : licenseExpirationDate,
    "businessName" : businessName,
    "ein" : ein,
    "organizationType" : organizationType,
    "ciy" : ciy,
    "state" : state,
    "zipCode" : zipCode,
    "startDate" : startDate,
    "endDate" : endDate
}
return searchBO;
}

SearchBO.java

import java.io.Serializable;

public class SearchBO implements Serializable {
private String individual;
private String organization;
private String enrollmentId;
private String medicareId;
private String npi;
private String contractId;
private String firstName;
private String lastName;
private String ssn;
private String dob;
private String licenseNumber;
private String licenseState;
private String specialtyType;
private String licenseExpirationString;
private String businessName;
private String ein;
private String organizationType;
private String ciy;
private String state;
private String zipCode;
private String startDate;
private String endDate;

public String isIndividual() {
    return individual;
}

public void setIndividual(String individual) {
    this.individual = individual;
}

public String isOrganization() {
    return organization;
}

public void setOrganization(String organization) {
    this.organization = organization;
}

public String getEnrollmentId() {
    return enrollmentId;
}

public void setEnrollmentId(String enrollmentId) {
    this.enrollmentId = enrollmentId;
}

public String getMedicareId() {
    return medicareId;
}

public void setMedicareId(String medicareId) {
    this.medicareId = medicareId;
}

public String getNpi() {
    return npi;
}

public void setNpi(String npi) {
    this.npi = npi;
}

public String getContractId() {
    return contractId;
}

public void setContractId(String contractId) {
    this.contractId = contractId;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getSsn() {
    return ssn;
}

public void setSsn(String ssn) {
    this.ssn = ssn;
}

public String getDob() {
    return dob;
}

public void setDob(String dob) {
    this.dob = dob;
}

public String getLicenseNumber() {
    return licenseNumber;
}

public void setLicenseNumber(String licenseNumber) {
    this.licenseNumber = licenseNumber;
}

public String getLicenseState() {
    return licenseState;
}

public void setLicenseState(String licenseState) {
    this.licenseState = licenseState;
}

public String getSpecialtyType() {
    return specialtyType;
}

public void setSpecialtyType(String specialtyType) {
    this.specialtyType = specialtyType;
}

public String getLicenseExpirationString() {
    return licenseExpirationString;
}

public void setLicenseExpirationString(String licenseExpirationString) {
    this.licenseExpirationString = licenseExpirationString;
}

public String getBusinessName() {
    return businessName;
}

public void setBusinessName(String businessName) {
    this.businessName = businessName;
}

public String getEin() {
    return ein;
}

public void setEin(String ein) {
    this.ein = ein;
}

public String getOrganizationType() {
    return organizationType;
}

public void setOrganizationType(String organizationType) {
    this.organizationType = organizationType;
}

public String getCiy() {
    return ciy;
}

public void setCiy(String ciy) {
    this.ciy = ciy;
}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public String getZipCode() {
    return zipCode;
}

public void setZipCode(String zipCode) {
    this.zipCode = zipCode;
}

public String getStartDate() {
    return startDate;
}

public void setStartDate(String startDate) {
    this.startDate = startDate;
}

public String getEndDate() {
    return endDate;
}

public void setEndDate(String endDate) {
    this.endDate = endDate;
}

@Override
public String toString() {
    return "SearchBO [individual=" + individual + ", organization="
            + organization + ", enrollmentId=" + enrollmentId
            + ", medicareId=" + medicareId + ", npi=" + npi
            + ", contractId=" + contractId + ", firstName=" + firstName
            + ", lastName=" + lastName + ", ssn=" + ssn + ", dob=" + dob
            + ", licenseNumber=" + licenseNumber + ", licenseState="
            + licenseState + ", specialtyType=" + specialtyType
            + ", licenseExpirationString=" + licenseExpirationString
            + ", businessName=" + businessName + ", ein=" + ein
            + ", organizationType=" + organizationType + ", ciy=" + ciy
            + ", state=" + state + ", zipCode=" + zipCode + ", startDate="
            + startDate + ", endDate=" + endDate + "]";
}

}

我使用以下方法将此对象提交给后端:

function setUpSearchBtn() {
$('#searchBtn').click(
        function() {
            var searchBO = getSearchBO();

            var data = JSON.stringify(searchBO);

            alert(data);

            var table = $('#searchResultTable').dataTable(
                    {
                        "processing" : true,
                        "destroy" : true,
                        "dom" : 'trtip',
                        "ajax" : {
                            "contentType" : "application/json",
                            "dataType" : 'json',
                            "url" : "search.jax",
                            "type" : "post",
                            "data" : data
                        },

                        "fnRowCallback" : function(nRow, aData,
                                iDisplayIndex) {
                            $('td:eq(1)', nRow).html(
                                    '<a href="profile.htm">' + aData[2]
                                            + '</a>');
                            return nRow;
                        },
                    });
        });

}

在java代码中,我使用:

@RequestMapping(value = "/search.jax", method = RequestMethod.POST)
@ResponseBody
public List<EntityProfileBO> doSearch(
@RequestBody SearchBO searchBO, Pagination pagination,
        HttpServletRequest request, HttpServletResponse response)
        throws APSException {
    System.out.println(searchBO.toString());
    List list = searchService.getSearch(searchBO, pagination);
    return list;
}
在chrome调试窗口中的

,我看到了:

Request Payload: 0=%7B&1=%22&2=i&3=n&4=d&5=i&6=v&7=i&8=d&9=u&10=a&11=l&12=%22&13=%3A&14=%22&15=f&16=a&17=l&18=s&19=e&20=%22&21=%2C&22=%22&23=o&24=r&25=g&26=a&27=n&28=i&29=z&30=a&31=t&32=i&33=o&34=n&35=%22&36=%3A&37=%22&38=f&39=a&40=l&41=s&42=e&43=%22&44=%2C&45=%22&46=e&47=n&48=r&49=o&50=l&51=l&52=m&53=e&54=n&55=t&56=I&57=d&58=%22&59=%3A&60=%22&61=%22&62=%2C&63=%22&64=m&65=e&66=d&67=i&68=c&69=a&70=r&71=e&72=I&73=d&74=%22&75=%3A&76=%22&77=%22&78=%2C&79=%22&80=n&81=p&82=i&83=%22&84=%3A&85=%22&86=%22&87=%2C&88=%22&89=c&90=o&91=n&92=t&93=r&94=a&95=c&96=t&97=I&98=d&99=%22&100=%3A&101=%22&102=%22&103=%2C&104=%22&105=f&106=i&107=r&108=s&109=t&110=N&111=a&112=m&113=e&114=%22&115=%3A&116=%22&117=%22&118=%2C&119=%22&120=l&121=a&122=s&123=t&124=N&125=a&126=m&127=e&128=%22&129=%3A&130=%22&131=%22&132=%2C&133=%22&134=s&135=s&136=n&137=%22&138=%3A&139=%22&140=%22&141=%2C&142=%22&143=d&144=o&145=b&146=%22&147=%3A&148=%22&149=%22&150=%2C&151=%22&152=l&153=i&154=c&155=e&156=n&157=s&158=e&159=N&160=u&161=m&162=b&163=e&164=r&165=%22&166=%3A&167=%22&168=%22&169=%2C&170=%22&171=l&172=i&173=c&174=e&175=n&176=s&177=e&178=S&179=t&180=a&181=t&182=e&183=%22&184=%3A&185=%22&186=%22&187=%2C&188=%22&189=s&190=p&191=e&192=c&193=i&194=a&195=l&196=t&197=y&198=T&199=y&200=p&201=e&202=%22&203=%3A&204=%22&205=%22&206=%2C&207=%22&208=l&209=i&210=c&211=e&212=n&213=s&214=e&215=E&216=x&217=p&218=i&219=r&220=a&221=t&222=i&223=o&224=n&225=D&226=a&227=t&228=e&229=%22&230=%3A&231=%22&232=%22&233=%2C&234=%22&235=b&236=u&237=s&238=i&239=n&240=e&241=s&242=s&243=N&244=a&245=m&246=e&247=%22&248=%3A&249=%22&250=%22&251=%2C&252=%22&253=e&254=i&255=n&256=%22&257=%3A&258=%22&259=%22&260=%2C&261=%22&262=o&263=r&264=g&265=a&266=n&267=i&268=z&269=a&270=t&271=i&272=o&273=n&274=T&275=y&276=p&277=e&278=%22&279=%3A&280=%22&281=%22&282=%2C&283=%22&284=c&285=i&286=y&287=%22&288=%3A&289=%22&290=%22&291=%2C&292=%22&293=s&294=t&295=a&296=t&297=e&298=%22&299=%3A&300=%22&301=%22&302=%2C&303=%22&304=z&305=i&306=p&307=C&308=o&309=d&310=e&311=%22&312=%3A&313=%22&314=%22&315=%2C&316=%22&317=s&318=t&319=a&320=r&321=t&322=D&323=a&324=t&325=e&326=%22&327=%3A&328=%22&329=1&330=4&331=%2F&332=1&333=0&334=%2F&335=2&336=0&337=1&338=4&339=%22&340=%2C&341=%22&342=e&343=n&344=d&345=D&346=a&347=t&348=e&349=%22&350=%3A&351=%22&352=%22&353=%7D

当我试图发布这些数据时,我也遇到了错误:

400 Bad Request

在服务器端,没有打印出来,这意味着根本没有到达控制器。 我的ajax代码有什么问题,我可以做些什么修改来解决它?谢谢高级!

1 个答案:

答案 0 :(得分:1)

我不知道它是否有效,但您可以尝试逐个发送值,如

var table = $('#searchResultTable').dataTable(
    {
        "processing" : true,
        "destroy" : true,
        "dom" : 'trtip',
        "ajax" : {
            "url" : "search.jax",
            "type" : "post",
            "data" : { individual: true, organization: true }