views:

76

answers:

6

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!

A: 

in the interest of a sanity check, try adding a beforeSend to your options and ensure the values are being sent and go from there....

e.g.

.ajax({
    beforeSend: function (xhr) {
  // this==the options for this ajax request
  if(! fb_post_date || !task_fb_postId || ! sedia_task_guid){
    alert("BORKED!");
  }
},
....
Sky Sanders
A: 

or you could try console.log(variable1,variable2) if your using firebug, to check values of the variables.

Jinah Adam
A: 

Check the DOM tab in Firebug and confirm that the values are populated. They might not be getting assigned properly.

jomanlk
+1  A: 

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

Jason Jong
+2  A: 

You could try some things such as:

  • See if all variables have values
  • Try to remove the "_" from the variable's names
AndreMiranda
It ended up being that some values were undefined even though they were initialized to empty strings and not changed... which is weird in itself. But I didn't want to spend time on it (I'll go back + see why later)... so I just added a check for undefined variables.
rksprst
+1  A: 

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

Hope this helps.

Raja