2015-02-23 22 views
0

我已經和Ajax請求的數據屬性JSON元素,我將數據傳遞給這樣的數據屬性:爲什麼我不能發送URL作爲傳遞給阿賈克斯

var myData = { "Identifier": 12312312, 
       "Description": "description", 
       "Name": "name", 
       "ImageUlr": "http://icons.iconarchive.com/icons/rob-sanders/hat/256/Hat-baseball-red-icon.png", 
       "Price": "price" }; 

$.support.cors = true; 


$.ajax({ type: 'POST', 
     dataType: 'json', 
     url: myUrl + categoryId, 
     data: JSON.stringify(myData), 
     contentType: 'application/json', 
     success: function (returnedData) {}, 
     error: function (xhr, ajaxOptions, thrownError) {}, 
     processData: false, 
     async: false 
     }); 

而當它進入服務器(Web Api),ImageUrl爲空。這裏是我的服務器端:

public IHttpActionResult PostProduct ([FromBody] Product postedProduct, string categoryId) 
{ 
    this.dbManager.PushNewProductInCategory(postedProduct, categoryId); 
    return Ok(); 
} 

回答

0

data: JSON.stringify(myData)應該是data:myData,你不應該stringify它作爲POST AJAX requests數據應該是一個有效的object

而且他們是在myDataImgUlr錯字應該是ImgUrl

1

嗯,我不能100%肯定,這將解決你的問題,你有一個錯字「ImageUlr」

2

無需字符串化數據,你應該像下面這樣以json格式發送它,並且可以工作。

var myData = { "Identifier": 12312312, "Description": "description", "Name": "name", "ImageUrl": "http://icons.iconarchive.com/icons/rob-sanders/hat/256/Hat-baseball-red-icon.png", "Price": "price" }; 

$ .support.cors = true;

 $.ajax({ 
      type: 'POST', 
      dataType: 'json', 
      url: myUrl + categoryId, 
      data: myData, 
      contentType: 'application/json', 
      success: function (returnedData) {}, 
      error: function (xhr, ajaxOptions, thrownError) {}, 
      processData: false, 
      async: false 
     });