0
我有問題的我的AJAX API函數之一:數據發送被檢索爲空在控制器
var baseURL = "http://localhost:8208/api/";
function updateMultipleQuestions(questionsPrototype, callback) {
var url = baseURL + "Questions?isMultiple=true";
$.ajax({
url: url,
type: 'PUT',
data: questionsPrototype,
dataType: 'json',
contentType: 'application/json',
statuscode: {
200: function (output) {
callback(output);
},
201: function (output) {
callback(output);
}
}
});
}
的questionsPrototype是類型MultiQuestion的一個目的是陣列問題:
function Question()
{
this.ID = 0;
this.Index = 0;
this.ImageURL = "imgurl_?";
this.Title = "";
this.QuestionText = "";
this.SectionID = 0;
this.CommentLength = 0;
this.Required = false;
this.NumberOfAllowedAnswers = 0;
this.NumberOfAllowedCharacters = 63;
this.OptionChoices = [];
this.QuestionType = 0;
}
function MultiQuestions()
{
this.Questions = [];
}
這個數據應該被髮送到控制器:
// PUT: api/Questions?isMultiple=true
[ResponseType(typeof(MultiQuestionActivityModel))]
public IHttpActionResult PutQuestion([FromUri] bool isMultiple, MultiQuestionActivityModel Questions)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
using (var uow = new UnitOfWorkFactory().Create())
{
var QuestionActivity = new QuestionActivity(uow);
if (QuestionActivity.MultiQuestionSave(Questions) > 0)
{
return Ok(Questions);
}
else
{
// Handle this
return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not save questions")); ;
}
}
}
現在在p問題是,當我調用API函數updateMultipleQuestions時,將正確的數據發送到函數中,但是從ajax調用到控制器的數據已成爲空(MultiQuestionActivityModel Questions)(在調試模式下檢查)。
我在Postman中測試了相同的數據,並調用它應該這樣做,這使我相信問題出在AJAX調用,可能其中一個頭部參數可能會使用錯誤?
我對此很新,可能忽略了一些東西。 事先致謝 /Janmumrik
我可以發誓,我已經嘗試了多次,但我可能已經做了這些,同時測試了不同的頭參數。似乎像魅力一樣工作,謝謝! – Janmumrik 2014-10-20 14:12:11