使用Ajax POST将Javascript数组传递给C#代码隐藏
Passing Javascript Array to C# Code Behind using Ajax POST
我正在尝试使用ajax post-back将javascript数组传递给C#代码。但当我尝试这个时,它没有起作用。我在下面附上了我的代码。有人能帮我吗??
test.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="hotelbeds.test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Tooltip - Custom animation demo</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script type="text/javascript">
function testbook(hotelcode) {
alert("clicked");
//var values = {"1","2","3"};
var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";
var theIds = JSON.stringify(mycars);
alert(theIds);
$.ajax({
type: "POST",
url: "test.aspx/Done",
contentType: "application/json; charset=utf-8",
data: "{ 'ids':'"+ theIds +"'}",
dataType: "json",
success: function (result) {
alert('Yay! It worked!');
},
error: function (result) {
alert('Oh no :(');
}
});
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
代码隐藏文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Services;
using System.Configuration;
using System.Drawing;
namespace hotelbeds
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs ea)
{
ImageButton testbtn = new ImageButton();
testbtn.OnClientClick = "return testbook('15000')";
form1.Controls.Add(testbtn);
}
[WebMethod]
public static void done(string[] ids)
{
String[] a = ids;
}
}
}
当我按照下面的方式更改网络方法时,它没有任何问题。但对我来说没用。
[WebMethod]
public static void done(string ids)
{
String a = ids;
}
错误的json格式:
"{ 'ids':'"+ theIds +"'}"
使用双引号。或者做得更好:
JSON.stringify({ ids: mycars });
试试这个
[WebMethod]
public static void done(List<string> ids)
{
String[] a = ids.ToArray();
}
试试这个。我已经成功地传递了javascript数组。唯一的限制是带有params关键字的对象必须是传递给web方法的最后一个参数。
http://msdn.microsoft.com/en-us/library/w5zay9db.aspx
[WebMethod]
public static void PassArray(params object[] list)
{
int i = 0;
while (i < list.Length)
{
//do your thing here
i++
}
}
相关文章:
- 从var向代码隐藏函数传递值
- javascript函数和代码隐藏函数的执行顺序
- 显示生成的gif;base64图像使用C#代码隐藏在img中使用JavaScript
- 使用较少代码隐藏和显示选择菜单内容的更好方法是什么?javascript
- 使用C#将在JavaScript中创建的变量值传递给ASP.Net中的服务器端(代码隐藏)
- 使用ScriptManager.RegisterStartupScript从代码隐藏调用javascript方法
- 正在代码隐藏中检索选定ListBox项的属性数据
- 为什么需要从代码隐藏调用 JavaScript 函数
- 空引用错误 Eval(字符串) 将其传递给代码隐藏中的函数
- 在代码隐藏 (asp.net VB) 中返回“完整”客户端 ID
- 使用 C# 中的代码隐藏在单击按钮时创建 Javascript 警报
- 如何从 C# 代码隐藏调用 JavaScript 函数
- 从 asp.net 代码隐藏中触发JavaScript函数
- ASP:代码隐藏的字段变量在从更新面板中的服务器返回时丢失值
- (“NetworkError:401未经授权)从JavaScript调用代码隐藏方法
- 如何将代码隐藏信息传递到javascript函数中
- 脚本执行,但代码隐藏不执行
- 无法访问字段'通过aspx页面上的javascript函数传递来自代码隐藏中函数调用的客户端id的s值(页面加载
- 访问子窗体的asp代码隐藏中的javascript window.myvariable
- 从Java脚本调用C#代码隐藏代码