【问题标题】:Send an Object and a string variable on Jquery Ajax在 Jquery Ajax 上发送一个对象和一个字符串变量
【发布时间】:2018-02-17 19:10:10
【问题描述】:

好的,我是一个新的 Ajax 调用,我想知道是否可以在 Ajax 调用中发送多个参数。

在我的$.Ajax 中,我正在发送一个对象和一个字符串。

 $.ajax({
            type: 'POST',
            url: urlAction,
            cache: false,
            data: JSON.stringify(customer,telephone),
            contentType: "application/json; charset=utf-8",
            success: function (data) {
            //Do something
            },
            error: function (xhr, ajaxOptions, thrownError) {
            //Do something
            }
        });

是否可以在data 中发送多个参数?或者在这种情况下,最好将telephone 视为customer DTO 的一部分?

我的JsonResult 正在尝试接收customerstring telephone,但电话无效

【问题讨论】:

  • 例如,您可以发送一个包含您的数据的对象。

标签: jquery ajax string object


【解决方案1】:

当您使用JSON.stringify 时,您需要确保传入一个对象。所以在这种情况下,你可以这样做:

JSON.stringify({customer: customer, telephone: telephone})

您可以在该对象中传递您想要的任何信息:

JSON.stringify({customer: customer, telephone: telephone, name: 'Carl'})

然后在你的服务器上(不管是什么代码,以 Ruby 为例)你可以访问它:

params[:customer]
params[:telephone]
params[:customer][:name]

只是为了好玩的 PHP 示例:

$_POST['customer'];
$_POST['telephone'];
$_POST['customer']['name'];

^ 我认为这仍然是有效的 PHP,已经有几年了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 2017-02-11
    • 2016-08-03
    • 2011-12-25
    • 1970-01-01
    相关资源
    最近更新 更多