【问题标题】:How to use jQuery Ajax post response params [duplicate]如何使用 jQuery Ajax 发布响应参数 [重复]
【发布时间】:2015-06-10 19:28:54
【问题描述】:

我想使用在 Ajax 响应中收到的参数,但我将其作为简单字符串接收。

阿贾克斯:

$.ajax({
    type: 'POST',
    url: url,
    data: str
}).done(function(res){
    // res is a simple string
    // => ResponseCode=0&Description=OK
});

假设我需要使用 Description 参数的值 (OK)。

【问题讨论】:

  • 使用dataType 属性接收所需格式的响应。
  • 我可能错了,但必须使用脏拆分并挖掘值..如果您可以改进响应字符串的格式会更好..
  • How can I get query string values in JavaScript? 的副本。您收到的响应 res 包含 URL 查询参数。查看提供的链接以找到解析它的解决方案。

标签: javascript jquery ajax


【解决方案1】:
$.ajax({
    type: 'POST',
    url: url,
    dataType : "json", // define type 
    data: str,
    error : function( er ){ console.log( e );},
    success: function( response ){

    console.log( response );
});

【讨论】:

  • 这应该适用于它可能适用于 JSON 的 din 现实,但将 json 用于您的东西,因为它是这样记录的。有助于避免混淆。我现在没有办法测试这些。但是好东西。
  • OP 没有使用 JSON。
  • @KAG OP 的响应 ResponseCode=0&Description=OK 不是 JSON。这是 URL 查询参数的格式。将 dataType 设置为 json 会导致错误,因为响应无法解析为 JSON。
猜你喜欢
  • 2018-12-02
  • 1970-01-01
  • 2023-03-22
  • 2011-09-09
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-17
相关资源
最近更新 更多