2013-05-13 155 views
0

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

回答

1

您作爲AJAX POST方法GET發送的參數。

您必須郵寄參數爲:

data: { key: value} 

格式。

+0

不,我在ajax請求中發送參數作爲POST。 。數據被序列化使用jquery.serialize() – Atiq 2013-05-13 06:25:26

+0

是的方法是POST,但你爲GET請求風格'var參數=值+「&ID =」+ encodeURIComponent(id);'構造參數,您需要修改如上POST。 – kailash19 2013-05-13 06:34:15

+0

var values = $(「#dailyEntryForm」)。serializeArray(); values.push({name:「Id」,value:id})data:values;仍然得到相同的錯誤。 。 – Atiq 2013-05-13 06:43:32

0

如果使用serializeArray構建POST這可能是更好的參數

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

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

jQuery將轉換,當你通過parameters以Ajax調用的data性質適當編碼所有值。

+0

是的,我試過,但我仍然得到相同的錯誤。 。 – Atiq 2013-05-13 06:49:35

+0

沒有足夠的信息進一步調試:服務器錯誤是什麼?服務器希望接收發布數據的格式是什麼?所有必填字段都被傳遞了嗎?所有的HTML輸入都有一個名稱屬性以允許序列化? – thefrontender 2013-05-13 06:55:14

+0

好吧,它似乎工作,但現在錯誤是有點不同,服務器無法獲取任何參數作爲json傳遞,它顯示爲每個參數爲空值,但實際數據在POST傳遞。 。 – Atiq 2013-05-13 06:59:22

相關問題