c# - 使用 Ajax POST 将 Javascript 数组传递到 C# 代码隐藏

标签 c# javascript asp.net ajax

我正在尝试使用 ajax 回发将 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]
        public static void done(string ids)
        {
            String a = ids;

        }

最佳答案

json 格式错误:

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

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

JSON.stringify({ ids: mycars });

关于c# - 使用 Ajax POST 将 Javascript 数组传递到 C# 代码隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18354676/

相关文章:

javascript - 查找字段数据类型不为 NaN 的所有记录

c# - 从 ASP.NET 页面中获取 Windows 登录

c# - 实际负责下载数据的 GetResponseStream() 或 ReadBytes() 以及如何下载数据?

javascript - 使用preventDefault

javascript - 如何存储不重置的测验的 JavaScript 计时器倒计时

asp.net - 当放置在 ASP.NET 应用程序的 Bin 文件夹中时, native DLL 会发生什么情况?

c# - Asp.net 开源项目作为 c# 中的学习资源

c# - 如何从 C# TcpListener 使用 jQuery ajax 读取 HTTP 响应?

c# - ASP.NET Core Mvc 中的邮件确认问题

c# - 如何在 .Net 中验证 Mongo ObjectId