Ajax,传递已编码的值

huangapple go评论65阅读模式
英文:

Ajax, pass already encoded value

问题

我试图通过AJAX数据传递编码值,但它继续进一步编码。如何阻止在AJAX中进行编码?

我的ajax代码:

$.ajax({
    url: form.attr("action"),
    data: {
        'at': ajax_params['at'],
    },
    type: form.attr("method"),
    success: function (data) {
        alert('Success');
    }
});

ajax_params['at'] 中的值为 2%2C+4,其中 2 和 4 是数值,%2C 表示逗号,+ 表示空格。该值可能会更长,包含多个逗号。

在提交AJAX后的当前输出为:

at=2%252C%2B4

期望的输出:

at=2%2C+4

英文:

I'm trying to pass encoded value through AJAX data but it keeps encoding it further. How can I prevent encoding in AJAX?

My ajax code:

    $.ajax({
        url: form.attr("action"),
        data: {
            'at': ajax_params['at'],
        },
        type: form.attr("method"),
        success: function (data) {
            alert('Success');
        }
    });

Value in ajax_params['at'] = 2%2C+4

where 2 and 4 is value, %2C is comma and + is space. The value can be longer with multiple commas.

Current output after AJAX submit:

> at=2%252C%2B4

Expected output:

> at=2%2C+4

答案1

得分: 1

你可以使用字符串来设置 data

data: "at=" + ajax_params['at'],

或者解码它,这样当它再次编码时,它不会被双重编码:

data: {
    'at': decodeURIComponent(ajax_params['at'])
},

另外注意:在属性 at 的属性名字面量或在查找 ajax_params 时,你不需要使用方括号和引号:

data: {
    at: decodeURIComponent(ajax_params.at)
},

at 在JavaScript中不是关键字或保留字(即使它是,自从ES5以来——早在2009年——在属性创建和访问表达式中使用不带引号的关键字是被允许的)。

英文:

You can either set data using a string:

data: "at=" + ajax_params['at'],

or decode it so that when it gets encoded again, it's not double-encoded:

data: {
    'at': decodeURIComponent(ajax_params['at'])
},

Side note: You don't need to use brackets notation and quotes for the property at, either in the property name in the literal or when looking it up on ajax_params:

data: {
    at: decodeURIComponent(ajax_params.at)
},

at isn't a keyword or reserved word in JavaScript (and even if it were, as of ES5 — back in 2009 — using unquoted keywords in property creation and access expressions was enabled in the language).

答案2

得分: 0

data: {
at: decodeURIComponent(ajax_params['at'])
},

英文:

Decode it before sending

data: {
  at: decodeURIComponent(ajax_params['at'])
},

huangapple
  • 本文由 发表于 2020年1月3日 19:17:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定