簡體   English   中英

使用 Ajax POST 將 Javascript 數組傳遞給 C# 代碼

[英]Passing Javascript Array to C# Code Behind using Ajax POST

我正在嘗試使用 ajax post back 將 javascript 數組傳遞給 C# 代碼。 但是當我嘗試這個時它沒有用。 我在下面附上了我的代碼。 任何人都可以幫我解決這個問題嗎??

測試.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 時,它沒有任何問題。 但這對我沒有用。

  [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++
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM