将webmethod参数传递给另一个Web方法 [英] passing webmethod parameter to another web method
本文介绍了将webmethod参数传递给另一个Web方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们如何使用第一个Web方法中的Ajax在另一个Web方法中调用一个Web方法,我需要调用另一个Web方法并将参数从第一个方法传递给第二个方法
how can we call one web method in another web method using ajax from the first web method i need to call another web method and pass the parameters from the first to second method
<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;
}
推荐答案
首先创建控制器
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();" />
这篇关于将webmethod参数传递给另一个Web方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文