如何使用第一个Web方法中的ajax在另一个Web方法中调用一个Web方法我需要调用另一个Web方法并将参数从第一个方法传递到第二个方法
<script type = "text/javascript">
function GetCityNameArray() {
var cities = ['Mumbai', 'Delhi', 'Chennai'];
PageMethods.GetCityNameArray(cities, OnSuccessGetCityNameArray);
}
function OnSuccessGetCityNameArray(response) {
for (var i in response) {
alert(response[i]);
}
}
</script>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true">
</asp:ScriptManager>
<div>
<input type = "button" onclick = "GetCityNameArray()" value = "Get City Name Array" />
</div>
</form>
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod]
public static string[] GetCityNameArray(string[] cities)
{
return cities;
}
答案 0 :(得分:0)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace DemoApp.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public JsonResult Check(string name, string age)
{
string[] abc = { name, age };
return Json(abc);
}
}
}
@{
ViewBag.Title = "Index";
}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20130526/json2.min.js"> </script>
<script type="text/javascript">
function GetData() {
var name = txtName.value;
var age = txtAge.value;
jQuery.ajax({
type: "Post",
url: '@Url.Action("Check", "Home")',
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ name: name, age: age }),
success: function (data) {
alert("Name=" + data[0] + " And " + "Age=" + data[1]);
}
});
return false;
}
</script>
Name :<input id="txtName" type="text" />
Age : <input id="txtAge" type="text" />
<input id="btnSubmit" type="button" value="button" onclick="return GetData();" />
答案 1 :(得分:0)
单击时,是否实际触发了单击功能?你调试了吗?如果没有,首先尝试在点击功能的第一行放置断点或添加一个简单的警报(“点击工作”),也许你的选择器是错误的。
答案 2 :(得分:0)
我们也可以在控制器中编写以下解决方案,并在视图侧传递到EmployeeDto到控制器。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace DemoApp.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public JsonResult Check(EmployeeDto EmployeeDto)
{
string[] abc = { EmployeeDto.name, EmployeeDto.age };
return Json(abc);
}
}
}
答案 3 :(得分:-1)
首先你可以进行一些修正,你需要document.ready事件或其他辅助函数(由于asp.net的clientid事情,我改为辅助函数)然后你必须停止按钮发回你的页面。
script type="text/javascript">
var onSubmitClick = function() {
var button = this;
var obj = {};
obj.name = $.trim($("[id*=txtName]").val());
obj.age = $.trim($("[id*=txtAge]").val());
$.ajax({
type: "POST",
url: "CS.aspx/SendParameters",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert(r.d);
}
});
return false;
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Name:
</td>
<td>
<asp:TextBox ID="txtName" runat="server" Text = "" />
</td>
</tr>
<tr>
<td>
Age:
</td>
<td>
<asp:TextBox ID="txtAge" runat="server" Text = ""/>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClientClick="return onSubmitClick();" UseSubmitBehaviour="false" />
</td>
</tr>
</table>
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod]
public static string SendParameters(string name, int age)
{
return string.Format("Name: {0}{2}Age: {1}", name, age, Environment.NewLine);
}