【问题标题】:ajax data parameter how to pass json including query-string urlsajax数据参数如何传递json,包括查询字符串url
【发布时间】:2015-06-04 07:16:53
【问题描述】:

我有一个问题。处理 json 。一切都很好,现在我知道如果我传递包含查询字符串 url 的 json 将不会像这样工作

这是一个json示例

var json='{"nodeDataArray": [ 
    {"info":"this si child 1 from step 1", "link":"https://www.google.co.in?check=abc&"}
     ]}'; 

我用ajax方法发送这个是代码

$.ajax({
        url: base_url + '/createflowchart/saveflowchart',
        type: 'POST',
        datatype: 'json',
        data: "flowchartname=" + flowchartname + "&chartcode=" + chartcode + "&content=" + json,
        beforeSend: function() {
            $('#SaveButton').text('saving...');

            // do some loading options
        },
        success: function(data) {
            //code
        },

        complete: function() {
            // success alerts
            $('#SaveButton').text('Save');
        },

        error: function(data) {
            alert("There may an error on uploading. Try again later");
        },

    });

现在问题来了

如果我传递包含这种类型的 url https://www.google.co.in?check=abc&check=2 的 json,它会将其拆分为带有 & 的 post 参数,但我不想要这个。 任何人都知道我怎么能做到这一点。任何帮助都会得到满足

【问题讨论】:

    标签: javascript jquery ajax json


    【解决方案1】:

    将您的data 参数作为对象提供,jQuery 将为您编码:

    data: {
        'flowchartname': flowchartname,
        'chartcode': chartcode,
        'content': json
    }
    

    因此,通常最好的做法是向$.ajax 调用以对象形式提供数据,而不是手动构建难看的查询字符串。

    【讨论】:

      【解决方案2】:

      修改了@rory 的答案

      data: {
              'flowchartname': flowchartname,
              'chartcode': chartcode,
              'content': json,
              },
      

      它对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多