【发布时间】:2017-06-16 09:14:27
【问题描述】:
我的问题是,当时我在浏览器控制台中将数据从 AJAX 调用到 ASP.NET C# 中的服务器端方法时显示错误,即 500 内部服务器错误。
这是我的客户端代码,
<label for="fname">Name</label>
<input type="text" id="txt_name" placeholder="Your Name.."/>
<label for="lname">EmailID</label>
<input type="text" id="txt_emailID" placeholder="Your EmailID.."/>
<label for="lname">Subject</label>
<input type="text" id="txt_subject" placeholder="Your Subject.."/>
<label for="subject">Content</label>
<textarea id="txt_content" placeholder="Write content.." style="height:200px"></textarea>
<input type="submit" id="submit_mail" value="Send"/>
<script>
$(function () {
$("#submit_mail").click(function () {
$.ajax({
type: "POST",
url: "contact.aspx/SendMail",
data: "{name: $('#txt_name').val(), email: $('#txt_emailID').val(), subject: $('#txt_subject').val(), message: $('#txt_content').val()}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
});
});
function OnSuccess(response) {
alert(response.d);
}
</script>
这是服务器端的代码
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
protected void SendMail(string name, string email, string subject, string message)
{
}
谢谢
【问题讨论】:
-
那是您的实际 C# 代码吗?另请注意,您发送的
data无效。您最好删除字符串周围的引号,并为 jQuery 提供一个可以自行序列化的对象。 -
是的,它只是实际的 c# 代码。你能告诉我我的代码的语法吗?
-
只需删除您提供给
data的值周围的"。然后你需要在生成有效响应的 C# 方法中实际放置一些逻辑 -
我做过这样的数据:{name: $('#txt_name').val(), email: $('#txt_emailID').val(), subject: $('# txt_subject').val(), message: $('#txt_content').val()},但同样的问题也来了
-
去掉引号后,您可以尝试在该对象上调用 JSON.stringify