Manoj Dhiman
Manoj Dhiman

Reputation: 5166

ajax data parameter how to pass json including query-string urls

I have a problem . Working on json . Everything was fine now i comes to know that if i pass json containing query-string urls will not work like this

here is a json example

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

i am sending this with ajax method below is the code

$.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");
        },

    });

now the issue is

if i pass json containg this type of url https://www.google.co.in?check=abc&check=2 it split this as a post parameter with & but i don't want this . Any body have idea how can i achive this .Any help will be appriciated

Upvotes: 0

Views: 4031

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337560

Supply your data parameter as an object and jQuery will encode it for you:

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

For this reason it's normally best practice to supply data to a $.ajax call in object form, instead of building an unsightly querystring manually.

Upvotes: 3

Manoj Dhiman
Manoj Dhiman

Reputation: 5166

Modified @rory's answer

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

it worked for me .

Upvotes: 0

Related Questions