【问题标题】:What is wrong with my Ajax code in my case using ASP.net?在我使用 ASP.net 的情况下,我的 Ajax 代码有什么问题?
【发布时间】:2017-10-11 09:30:27
【问题描述】:

我有这样的数据

[{"billno":"111","amount":"2233.00"},{"billno":"222","amount":"2500.00"},{"billno":"333","amount":"3000.00"}]

我想将此记录存储在我的数据库中,所以在此之前我试图将这些记录发送到服务器

这是我的 AJAX 代码:

$('#btnAddVendor').click(function () {


    var values = [];

    $('table#ContentPlaceHolder1_GridView1 input.checkBoxClass:checked').each(function () {
        var $row = $(this).closest('tr').children('td');
        values.push({ 'billno': $row.eq(1).text(), 'amount': $row.eq(5).text() });
    });

    //html_data = JSON.stringify(values);

    alert(JSON.stringify(values));  // this alert will display above values

    $.ajax({
        type: 'POST',
        url: 'GPCreateCheque.aspx/setCheqVendorSearchEntry',
        data: JSON.stringify(values),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (result) {
            alert(result.d);
        },
        error: function (result) {
                    alert("Not save");
                }
    });
})

WebMethod 代码

public partial class WebForm5 : System.Web.UI.Page
{
    [WebMethod]
    public static string setCheqVendorSearchEntry(vendorEntry[] values)
    {

        //here I will write the code to store the records in database

        return "Success";  //for testing I return this string
    }
}

public class vendorEntry{
    public string billno { get; set; }
    public string amount { get; set; }
}

我不知道如何从 ajax 接收。谢谢

更新错误消息

http://localhost:55047/GPCreateCheque.aspx/setCheqVendorSearchEntry 500 (Internal Server Error)

【问题讨论】:

  • 错误是什么? 404页面? venderEntrys 为空?
  • @GGO Status:500 Internal server error
  • 为什么要将 json 转换为字符串。将其作为 json 作为数据传递:值。
  • @UbiquitousDevelopers 如何在服务器端传递和接收,这种数组值为json?
  • 错误:函数(结果){console.log(结果); } 检查您的浏览器控制台是否有确切的错误

标签: c# jquery asp.net json ajax


【解决方案1】:

我终于让它运行了:

你需要让它允许 POST 方法

[System.Web.Services.WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
public static string setCheqVendorSearchEntry(vendorEntry[] values)
{
    return "Success";  //for testing I return this string
}

和 Default.aspx,您的 Json 数组不正确。您需要在 json 元素中获取参数名称(这里是 setCheqVendorSearchEntry 方法中的“值”)并将其作为字符串或序列化传递。

 var values = '{ "values": [{ "billno": "111", "amount": "2233.00" }, { "billno": "222", "amount": "2500.00" }, { "billno": "333", "amount": "3000.00" }] }';

                    $.ajax({
                        type: 'POST',
                        url: 'Default.aspx/setCheqVendorSearchEntry',
                        data: values,
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        success: function (result) {
                            alert(result.d);
                        },
                        error: function (result) {
                            console.log(result);
                        }
                    });

为了您的学习目的:

当您在 ajax 错误部分中控制台您的错误时,您可以在浏览器控制台中找到确切的错误,如下所示: 图片:https://prnt.sc/gw06sr

【讨论】:

  • 我可以这样使用data: JSON.stringify(values)吗?
  • 是的,您可以使用,但您必须为此从 Json 中删除单引号
  • 你说的是哪个单引号。
  • var values = { "values": [{ "billno": "111", "amount": "2233.00" }, { "billno": "222", "amount": "2500.00 " }, { "billno": "333", "amount": "3000.00" }] };你可以这样使用
  • 如果我的解决方案有效,您可以投票并接受它作为答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-25
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 2011-07-28
相关资源
最近更新 更多