C#返回json对象
我有一个我希望是相当直截了当的问题。我有一个联系人页面,我使用jquery函数将数据发送到c#sendmail方法。这一切都有效。我正在将信息发送回jquery,但遇到了问题 我的c#是: jquery的ajax部分是:C#返回json对象,c#,json,C#,Json,我有一个我希望是相当直截了当的问题。我有一个联系人页面,我使用jquery函数将数据发送到c#sendmail方法。这一切都有效。我正在将信息发送回jquery,但遇到了问题 我的c#是: jquery的ajax部分是: $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", processData: false, dataType: "json",
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
processData: false,
dataType: "json",
cache:false,
url: "x/sendEmail",
dataType: 'json',
data: JSON.stringify(myData),
//complete changed to success
success: function (response) {
if (response != null && response.success) {
alert(response.success + " pass" + response.responseText);
} else {
alert(response.success + " fail " + response.responseText);
}
},
error: function (response) {
alert(response.success + " fail2 " + response.responseText);
}
我得到的响应.success为true或false,但response.responseText始终为“未定义”
不知道我错过了什么
我稍微改变了C#但结果相同
public class ResponseObject
{
public bool success { get; set; }
public string responseText { get; set; }
}
public ActionResult sendEmail(string userName, string userEmail, string
userPhone, string userAddress,string userSubject, string userMessage, string
userList)
{
///code to send mail - works no problem
ResponseObject response;
if (!ok)
{
// Send "false"
response = new ResponseObject { success = false, responseText = "FAIL" };
}
else
{
// Send "Success"
response = new ResponseObject { success = true, responseText = "Your message successfuly sent!" };
}
return Json(response, JsonRequestBehavior.AllowGet);
}
按F12打开开发工具,转到“网络”选项卡,再次进行AJAX调用。现在,您应该能够找到调用sendEmail函数的url。单击它,转到“响应”选项卡,查看服务器发送的响应。在那里,您可以验证是否同时收到属性和所使用的格式(例如,属性名称中可能有输入错误)。尝试返回
内容(您将需要Newtonsoft.Json包)
你能在成功时键入console.log(response)
,看看它是否仍然包含未定义的内容吗?如果我在单击警报之前尝试,这不会给我任何信息-单击警报之后会给我未定义的信息。在我的ajax中,虽然它在显示alertOk之前显示为NOTNULL。。。在路上,但我回家后会试试。。。感谢我点击submit按钮,然后得到显示“true pass undefined”的警报。在单击警报之前,我正在检查“响应”选项卡,该选项卡为空。响应不是null,因为我测试了它,只是不确定responseText在哪里。我还尝试显式定义对象,如更新中所示。所有字段都传递intellisence以便拼写正确…?实际上,您需要在发送AJAX请求后查看响应。主要的想法是看看MVC是如何返回结果的,看看它与您期望的结果是否有任何不同。老实说,这里真正的答案是调试javascript代码,方法是在成功回调中设置断点,然后检查“response”变量的值,但由于您使用警报查看内容,我猜您仍然不熟悉调试工具。不,我不是很熟悉,我用得不够。我以前在jscript和jquery中使用过断点,但在Ajax和json中使用断点时遇到了问题。我今晚可以试试看会发生什么。感谢您可能的问题是您没有考虑AJAX请求是以异步方式执行的,因此您无法让它调试回调函数,因为它跳过了回调函数。为了避免这种情况,请在回调函数中设置断点,这样,一旦服务器返回响应,您就可以调试该函数。同样的事情success=true,但responseText未定义客户端的console.log(response)输出是什么??
public class ResponseObject
{
public bool success { get; set; }
public string responseText { get; set; }
}
public ActionResult sendEmail(string userName, string userEmail, string
userPhone, string userAddress,string userSubject, string userMessage, string
userList)
{
///code to send mail - works no problem
ResponseObject response;
if (!ok)
{
// Send "false"
response = new ResponseObject { success = false, responseText = "FAIL" };
}
else
{
// Send "Success"
response = new ResponseObject { success = true, responseText = "Your message successfuly sent!" };
}
return Json(response, JsonRequestBehavior.AllowGet);
}
public ActionResult sendEmail(string userName, string userEmail,string userPhone, string userAddress, string userSubject, string userMessage, string userList)
{
///code to send mail - works no problem
if (!ok)
{
// Send "false"
var response = new { success = false, responseText = "FAIL" };
return Content(Newtonsoft.Json.JsonConvert.SerializeObject(response), "application/json");
}
else
{
// Send "Success"
var response = new { success = true, responseText = "Your message successfuly sent!" };
return Content(Newtonsoft.Json.JsonConvert.SerializeObject(response), "application/json");
}
}