【发布时间】:2015-05-30 06:53:02
【问题描述】:
我在将 json 数组从 jquery 传递到我的 mvc5 项目中的 webapi 时遇到语法问题。
以下是我的代码:-
C#代码:-
//获取实例
// GET: api/PostDatas
public IQueryable<PostData> GetPostDatas()
{
return db.PostDatas;
}
//POST 实例
// POST: api/PostDatas
[ResponseType(typeof(PostData))]
public IHttpActionResult PostPostData(PostData postData)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.PostDatas.Add(postData);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = postData.postDataID }, postData);
}
jQuery
<script>
function fnpostdata() {
var model = {
"userid": "01",
"Description": "Desc",
"photoid": "03"
};
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
url: "/api/PostDatas/",
data: model,
success: function (data) {
alert('success');
},
error: function (error) {
jsonValue = jQuery.parseJSON(error.responseText);
jError('An error has occurred while saving the new part source: ' + jsonValue, { TimeShown: 3000 });
}
});
}
</script>
我无法使用 jquery 将数据发送到我的 c# 控制器,只需要了解语法。谢谢。
【问题讨论】:
-
你有什么错误吗?
-
虽然没有错误,但.. 实际上,我的数据没有发布到我的 c# 控制器中。
-
//GET INSTANCE 正在从数据库中检索数据,但是 //POST INSTANCE 在将数据发布到数据库时遇到了一些问题,需要帮助理解他的语法..
标签: c# jquery json asp.net-web-api