JavaScript AJAX variables truncation issue FIX

Suppose you might have heavy ajax processing app that communicate between your server and application mostly with large json data.

var search_results = [{
    "name": "Vikas",
    "profession": "Programmer",
    "age": "31"
}, {
    "name": "Amit",
    "profession": "Tester",
    "age": "26"
}, {
    "name": "Brett",
    "profession": "Admin",
    "age": "21"
}, {
    "name": "Dhaval",
    "profession": "Support",
    "age": "27"
}, {
    "name": "Matt",
    "profession": "Marketing",
    "age": "37"
}]

It might possible your browser truncate json string post variables. May be because it might contain unecaspped & and some other querystring breaking variables.

Initially i thought, it could be php.ini issue.

post_max_size = 8M

So, I tried to increase size of post_max_size directive and restarted apache web server, but nothing happened.

Then I realized that JSON encoding was wrong.

Try escape () or encodeURIComponent() over json post variables to overcome this issue.

$.ajax({
    url: "test1.php",
    type: "POST",
    data: "id=" + id + "&results=" + encodeURIComponent(JSON.stringify(search_results)),
}).done(function (result) {
    // do post ajax stuff here
});

That resolved my problem.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

 
Previous Post

How to track remote javascript errors using Google Analytics?

Next Post

Form auto-submission tips for jQuery

Related Posts