[英]MVC dynamic javascript with action call
Dynamic JavaScript on change event not firing I have my action result as follows 动态JavaScript发生变更事件未触发我的动作结果如下
public ActionResult About()
{
ViewBag.Script = "<input type='text' name='myName' onchange='nameChange(this)' /><br/>" +
"<script type='text/javascript'>function nameChange(d){" +
"$('[name=myEmail]').val('');" +
"$.ajax({type: 'POST', url: '/Home/Search', data: '{name:d}', " +
"contentType: 'application/json; charset=utf-8', dataType: 'html'," +
"success: function(data) { $('[name=myEmail]').val(data.toString()); } });" +
"}</script>"
+ "<input type='text' name='myEmail' />";
ViewBag.Message = "Your application description page.";
return View();
}
I have my controller as follows 我的控制器如下
public ContentResult Search(string name)
{
return Content("hello@gmail.com");
}
But the action url is not getting recognized can some one help me 但是动作网址没有得到认可,有人可以帮我吗
cshtml CSHTML
@if (ViewBag.Script != null)
{
@Html.Raw(ViewBag.Script)
}
With contentType: 'application/json; charset=utf-8'
使用contentType: 'application/json; charset=utf-8'
contentType: 'application/json; charset=utf-8'
you are defining that you will send Json
with the $.ajax
request but you are not. contentType: 'application/json; charset=utf-8'
您正在定义要向Json
发送$.ajax
请求,但不是。 You should make a Json
object by changing the $.ajax data
to: 您应该通过将$.ajax data
更改为以下内容来创建Json
对象:
data: JSON.stringify({name:d})
You have also defined a POST transaction in your $.ajax
so the Controller Action should be decorated with a [HttpPost]
attribute:- 您还已经在$.ajax
定义了POST事务,因此Controller Action应该用[HttpPost]
属性修饰:-
[HttpPost]
public ContentResult Search(string name)
{
return Content("hello@gmail.com");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.