我希望使用从MVC到Post API的ajax调用来发布数据。数据已成功发布,但ajax调用始终会触发错误功能警报。
查看页面
<div class="input-group">
@Html.TextBoxFor(m => m.ConfirmVisitID, new { Class = "form-control input-circle-right", @readonly = "readonly", @id = "Cvid" })
</div>
//all the input fields comes here
<input value="Confirm" type="button" id="loccat" onclick="CheckPromotionalCode();">
脚本
<script>
function CheckPromotionalCode() {
var jva = document.getElementById('Mid').value;
var cid = document.getElementById('Cvid').value;
var pname = document.getElementById('PatientName').value;
var datefrom = document.getElementById('SDate').value;
var dateend = document.getElementById('EDate').value;
var mc = document.getElementById('IsMC').value;
var dept = document.getElementById('IsDepartment').value;
var empl = document.getElementById('IsEmployee').value;
var depen = document.getElementById('IsDependent').value;
var res = document.getElementById('reason').value;
var remark = document.getElementById('rem').value;
$.ajax({
url: 'http://localhost:89522/Mc/McIssued',
data: JSON.stringify({ Mid: jva, Cvid: cid, PatientName: pname, SDate: datefrom, EDate: dateend, IsMC: mc, IsDepartment: dept, IsEmployee: empl, IsDependent: depen, reason: res, rem: remark }),
method: 'post',
dataType: 'json',
contentType: 'application/json;charset=utf-8',
success: function (response) {
alert("Successfull");
window.location.href = response.Url;
},
error: function () {
alert("Not Successfull");
}
});
}
控制器
public ActionResult McIssued(string Cvid, string Mid, string SDate, string EDate, string reason, string rem, string IsMC, string IsDepartment, string IsEmployee, string IsDependent, string PatientName)
{
string url = "http://localhost:74523";
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
McIssued mc = new McIssued()
{
MemberID = Mid,
ConfirmVisitID = Cvid,
ProviderID = PID,
CreateUserID = UserId,
StartDate = StrtDate,
EndDate = EndDate,
McType = reason,
Remarks = rem,
RecStatus = Rec,
ModifyUserID = ModifyUserId
};
HttpResponseMessage response = client.PostAsJsonAsync<McIssued>("api/Mc/InsertintoMcTxn", mc).Result;
System.Diagnostics.Debug.WriteLine(response);
response.ToString();
response.EnsureSuccessStatusCode();
var redirectUrl = new UrlHelper(Request.RequestContext).Action("IssueMC", "Mc");
return Json(new { Url = redirectUrl });
}
那么,我该如何克服这个问题呢?任何帮助,将不胜感激。提前致谢