调用Web服务时出现400错误请求

时间:2013-05-13 05:53:15

标签: jquery jpa

我在使用jquery ajax post调用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();
              }
    }
}

2 个答案:

答案 0 :(得分:1)

您正在AJAX POST方法中将参数作为GET发送。

您必须将参数发送为:

data: { key: value}

格式。

答案 1 :(得分:0)

如果使用serializeArray构造POST参数

,可能会更好
var parameters = $("#dailyEntryForm").serializeArray();
var id = billingObject[localStorage.index].billingId;

parameters.push({name: "Id", value: id});

当您将parameters传递给ajax调用的data属性时,jQuery将适当地转换和编码所有值。