0

我在使用 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();
              }
    }
}
4

2 回答 2

1

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

您必须将参数发送为:

data: { key: value}

格式。

于 2013-05-13T06:02:39.067 回答
0

如果你用来serializeArray构造 POST 参数可能会更好

var parameters = $("#dailyEntryForm").serializeArray();
var id = billingObject[localStorage.index].billingId;

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

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

于 2013-05-13T06:43:42.360 回答