通过我正在使用的ajax请求将数据发送到控制器时遇到了一些麻烦。
目前在我看来,我有
<p>Please enter your email and phone number registered with the account</p>
<table id="Table">
<tr>
<td>Email</td>
<td> <input id = "email" type ="text" name= "email" /></td>
</tr>
<tr>
<td>Phone Number</td>
<td> <input id = "phone" type ="text" name= "phone" /></td>
</tr>
</table>
<input type="button" value="Submit" id="sendMSG">
和以下脚本
<script src="http://code.jquery.com/jquery-1.9.1.js">
</script>
<script>
$("#sendMSG").click(function () {
$.ajax({
type: "POST",
url: '@Url.Action("SendMessage", "SMSTest")',
dataType: "JSon",
data: { "email": $("#email").toString(), "phone": $("#phone").toString()},
success: function (data) {
console.log(data);
},
error: console.log("it did not work"),
});
});
</script>
当我调用该函数时,我将其发送到SMSTestController
public JsonResult SendMessage(string email, string phone)
{}
我目前收到的是电子邮件和手机的价值[object Object]
,这是我点击按钮后电子邮件和手机的类型字符串。
您是否知道如何将我在文本框中输入的实际字符串直接发送到控制器?
由于
答案 0 :(得分:3)
您的Javascript错误,您需要使用val():
<script>
$("#sendMSG").click(function () {
$.ajax({
type: "POST",
url: '@Url.Action("SendMessage", "SMSTest")',
dataType: "JSon",
data: { "email": $("#email").val(), "phone": $("#phone").val()},
success: function (data) {
console.log(data);
},
error: console.log("it did not work"),
});
});
</script>
答案 1 :(得分:3)
您没有抓取输入字段的值,而是将整个对象本身作为字符串发送。
尝试:
data: { "email": $("#email").val(), "phone": $("#phone").val()},