ajax数据参数如何传递json,包括query-string urls

时间:2015-06-04 07:16:54

标签: javascript jquery ajax json

我有问题。在json上工作。一切都很好,现在我知道,如果我传递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");
        },

    });

现在问题是

如果我传递json包含此类型的网址https://www.google.co.in?check=abc&check=2,则将其拆分为&的帖子参数,但我不想这样做。 任何一个团体都知道如何才能实现这一点。任何帮助都会得到满足

2 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:0)

修改@ rory的回答

data: {
        'flowchartname': flowchartname,
        'chartcode': chartcode,
        'content': json,
        },
它对我有用。