Not all parameters get sent in jquery ajax call

By : rksprst
Source: Stackoverflow.com
Question!

I have a strange error where my jquery ajax request doesn't submit all the parameters.

$.ajax({
    url: "/ajax/doAssignTask",
    type: 'GET',
    contentType: "application/json",
    data: { 
        "just_a_task": just_a_task,
        "fb_post_date": fb_post_date,
        "task_fb_postId": task_fb_postId,
        "sedia_task_guid": sedia_task_guid,
        "itemGuid": itemGuid,
        "itemType": itemType,
        "taskName": taskName,
        "assignedToUserGuid": assignedToUserGuid,
        "taskDescription": taskDescription
    },
    success: function(data, status) {
        //success code
    },
    error: function(xhr, desc, err) {
        //error code
    }
});

But using firebug (and debugging) I can see that only these variables are posted:

assignedToUserGuid
itemGuid
itemType
just_a_task
taskDescription 
taskName

It's missing fb_post_date, task_fb_postId, and sedia_task_guid

I have no idea what would cause it to post only some items and not others? Anyone know?

Data is sent to asp.net controller that returns jsonresult (hence the contentType)

Any help is appreciated. Thanks!

By : rksprst


Answers
You could try some things such as:

  • See if all variables have values
  • Try to remove the "_" from the variable's names


What you should to to help with coding and debug is move the JSON data to a variable... ie, then you can easily see what is inside the variable before posting

var myData = { 
    just_a_task: just_a_task,
    fb_post_date: fb_post_date,
    task_fb_postId: task_fb_postId,
    sedia_task_guid: sedia_task_guid,
    itemGuid: itemGuid,
    itemType: itemType,
    taskName: taskName,
    assignedToUserGuid: assignedToUserGuid,
    taskDescription: taskDescription
};
var jsonData = $.toJSON(myData);

$.ajax({
    url: "/ajax/doAssignTask",
    type: "GET",
    contentType: "application/json",
    dataType: "json",
    data: jsonData,
    success: function(data, status) {
        //success code
    },
    error: function(xhr, desc, err) {
        //error code
    }
});

Though I dont have time to run the code, could be the speech marks in the JSON. That should be out as its native JavaScript



Check for special characters in your data values (, { } [ ] " '). You have to escape those characters for JSON to work.

Hope this helps.

By : Raja


This video can help you solving your question :)
By: admin