我使用jquery ajax post調用Web服務時收到來自服務器的錯誤請求。 我的代碼如下所示: -400調用Web服務時的錯誤請求
$("#dailyEntryUpdate").click(function(){
event.preventDefault();
var values = $("#dailyEntryForm").serialize();
var id = billingObject[localStorage.index].billingId;
var parameters = values+"&Id=" + encodeURIComponent(id);
console.log(" values are "+ parameters);
$.ajax({
url: "http://localhost:8080/TIMS/rest/UpdateEntry/update",
type: "POST",
data: parameters,
success: function(){
alert("Record updated successfully");
},
error:function(){
alert("failure");
}
});
});
而服務器端看起來像這樣
@XmlRootElement
@Path("/UpdateEntry")
public class UpdateEntryService{
@POST
@Path("/update")
@Consumes("application/x-www-form-urlencoded")
public void updateBillingList(
@FormParam("truckNo") String truckNo,
@FormParam("Source") String source,
@FormParam("Destination") String destination,
@FormParam("BookingDate") String bookingDate,
@FormParam("UnloadingDate") String unloadingDate,
@FormParam("Weight") int weight,
@FormParam("Freight") float freight,
@FormParam("Advance") float advance,
@FormParam("Balance") float balance,
@FormParam("Commision") float commision,
@FormParam("Hamali") float hamali,
@FormParam("DelieveryCharge") float delieveryCharge,
@FormParam("Remarks") String remarks,
@FormParam("Detention") float detention,
@FormParam("Id") String id) {
EntityManager em = DaoHelper.getInstance().getEntityManager();
try{
em.getTransaction().begin();
Billing bill = em.find(Billing.class, id);
bill.setAdvance(advance);
bill.setBalance(balance);
bill.setCommision(commision);
bill.setDelieveryCharge(delieveryCharge);
bill.setDetention(detention);
bill.setFreight(freight);
bill.setHamali(hamali);
DailyEntry entry = bill.getEntry();
entry.setBookingDate(bookingDate);
entry.setDestination(destination);
entry.setRemarks(remarks);
entry.setSource(source);
entry.setTruckId(truckNo);
entry.setUnloadingDate(unloadingDate);
entry.setWeight(weight);
em.getTransaction().commit();
} finally {
em.close();
}
}
}
不,我在ajax請求中發送參數作爲POST。 。數據被序列化使用jquery.serialize() – Atiq 2013-05-13 06:25:26
是的方法是POST,但你爲GET請求風格'var參數=值+「&ID =」+ encodeURIComponent(id);'構造參數,您需要修改如上POST。 – kailash19 2013-05-13 06:34:15
var values = $(「#dailyEntryForm」)。serializeArray(); values.push({name:「Id」,value:id})data:values;仍然得到相同的錯誤。 。 – Atiq 2013-05-13 06:43:32