【问题标题】:Passing Javascript Array to C# Code Behind using Ajax POST使用 Ajax POST 将 Javascript 数组传递给 C# 代码
【发布时间】:2013-08-23 15:54:15
【问题描述】:

我正在尝试使用 ajax 回传将 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;

        }

【问题讨论】:

    标签: c# javascript asp.net ajax


    【解决方案1】:

    错误的json格式:

    "{ 'ids':'"+ theIds +"'}"
    

    使用双引号。或者做得更好:

    JSON.stringify({ ids: mycars });
    

    【讨论】:

      【解决方案2】:

      试试这个

          [WebMethod]
          public static void done(List<string> ids)
          {
              String[] a = ids.ToArray();
      
          }
      

      【讨论】:

        【解决方案3】:

        试试这个。我已经成功地传递了 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++
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2019-12-24
          • 1970-01-01
          • 1970-01-01
          • 2014-06-21
          • 2021-02-10
          • 2016-11-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多