Response "Error 400 - Bad Request"

I'm using a couple of ajax calls to gather data from a view (in my case view 200) and then place that information into two objects (in my case object 38 and object 15).

The first POST works fine.  The data arrives and the response is successful.

The second POST fails with an Error 400 - Bad Request.  It appears as though I lose my data when I go to do the second ajax call.  Here is the code I'm using.

___________________________________________________________________

// Zone4 Details Create New COMMON record when button pushed - Start of Code
// on page load
$(document).on('knack-view-render.view_200', function(event, view) {

// Add a buttons for adding Player, Guardian 1 and Guardian 2
$('<button id="Player"">Insert Player</button>').insertBefore('.field_377');
$('<button id="Guard1"">Insert Guardian 1</button>').insertBefore('.field_366');
$('<button id="Guard2"">Insert Guardian 2</button>').insertBefore('.field_371');

// Setup variables to collect Zone4 data
var zone4_player = Knack.models['view_200'].toJSON();
var zone4_guard1 = Knack.models['view_200'].toJSON();
var zone4_guard2 = Knack.models['view_200'].toJSON();

// Gather data for later use in ajax calls.
var player_first_name = zone4_player.field_377_raw.first;
var player_last_name = zone4_player.field_377_raw.last;
var player_home = zone4_player.field_379_raw.number;
var player_cell = 'NOT AVAILABLE';
var player_email = zone4_player.field_380_raw.email;
var player_street = zone4_player.field_378_raw.street;
var player_street2 = zone4_player.field_378_raw.street2;
var player_city = zone4_player.field_378_raw.city;
var player_prov = zone4_player.field_378_raw.state;
var player_postal = zone4_player.field_378_raw.zip;
var player_medconcerns = zone4_player.field_385_raw;
var player_healthnum = zone4_player.field_384_raw;
var player_communitynum = zone4_player.field_375_raw;
var player_dob = zone4_player.field_382_raw.date;
var player_gender = zone4_player.field_741_raw;


// event for the buttons when clicked
// Player button
$('#Player').click(function() {
alert ('Adding Player to Common Database');
Knack.showSpinner();

// set data variable for use in the ajax insert later
var commonplayerdata = {
field_590: {
first: player_first_name+'TEST',
last: player_last_name+'TEST'
},
field_597: 'Player',
field_591: {
street: player_street,
street2: player_street2,
city: player_city,
state: player_prov,
zip: player_postal
},
field_593: player_home,
field_594: player_email,
field_600: '5a3172ba279dac07130e5e9c' //High Park
};

// use an ajax call to insert the new common record into the common object (object 38)
$.ajax({
url: 'https://api.knackhq.com/v1/objects/object_38/records/',
type: 'POST',
headers: {
'X-Knack-Application-ID': 'APP ID',
'X-Knack-REST-API-Key': 'API KEY'
},
data: commonplayerdata,
// use data in success function to gather response from Knack
success: function(object38successdata)
{
// pop up a message box showing successful data insert and hide the Knack wait spinner
alert ("Object 38 Success: " + JSON.stringify(object38successdata));
alert ("Commonplayerdata: " + JSON.stringify(commonplayerdata));
// convert response data into variables
var ob38postrecid = object38successdata.id;
alert ("Object 38 Post Record ID: "+ ob38postrecid);

// Gather data to use in next insert into player users object (object 15)
// set data variable for use in the ajax insert later
var playeruserdata = {
field_166: {
first: player_first_name+'TEST',
last: player_last_name+'TEST'
},
field_167: player_email,
field_168: 'Password',
field_169: 'Active',
field_453: player_gender,
field_454: player_communitynum,
field_456: player_healthnum,
field_457: player_medconcerns,
field_464: '2018',
field_469: player_dob,
field_596: ob38postrecid
};
alert("Adding player to player user table (Object 15).");
alert("playeruserdata : " + JSON.stringify(playeruserdata));
alert ("Object 38 Post Record ID: "+ ob38postrecid);
$.ajax({
url: 'https://api.knackhq.com/v1/objects/object_15/records/',
type: 'POST',
headers: {
'X-Knack-Application-ID': 'APP ID',
'X-Knack-REST-API-Key': 'API KEY'
},
data: playeruserdata,
success: function(object15successdata)
{
// pop up a message box showing successful data insert and hide the Knack wait spinner
alert ("Object 15 Success: " + JSON.stringify(object15successdata));
// convert response data into variables
var ob15postrecid = object15successdata.id;
alert ("Object 15 Post Record ID: "+ ob15postrecid);
},
error: function(object15errordata)
{
// pop up a message box if the post failed and hide the Knack wait spinner
alert("Object 15 Error: " + JSON.stringify(object15errordata));
alert("playeruserdata: " + JSON.stringify(playeruserdata));
alert ("Object 38 Post Record ID: "+ object38successdata.id);
Knack.hideSpinner();
}
});
//We are now back inside the success loop from Object 38's ajax call.
Knack.hideSpinner();
},
error: function(object38errordata)
{
// pop up a message box if the post failed and hide the Knack wait spinner
alert("Object 38 Error: " + JSON.stringify(object38errordata));
Knack.hideSpinner();
}
// Close line 226
});

// Close line 199
});

Hmm understand Jeff ..

I figured it out.  It was because I was trying to insert text into an eMail field.  As soon as I added   .email  to the formatting code it worked out fine.

 

Here is the updated code.

 

var playeruserdata = {
field_166: {
first: player_first_name+'TEST',
last: player_last_name+'TEST'
}, // Name
field_504: '5a3172ba279dac07130e5e9c', // Community Connection
field_453: player_gender, //Gender
field_167: player_email.email, // eMail
field_168: "SoftballPassword", // Temporary Password
field_169: "Active", // User Status
filed_170: "Player", // User Roles
field_454: player_communitynum, // Community League Number
field_456: player_healthnum, // Health Number
field_457: player_medconcerns, // Medical Concerns
field_464: '2018', // Registration Year Hard Coded
field_742: player_dob, // Date of Birth
field_596: ob38postrecid // Common Connection
};

field_168: 'Password',

Hello Jeff

I think it's password issue may be you set some password validations.

Can you try this using exact value of password with meet requirements . Like if you set 1 numeric then add number etc .

And can you share your post data string ?

Thanks
Sunny Singla

Sent that too fast... does anyone see anything wrong with my java?  Anyone have any ideas why this code might work on the first object (38) but fail on the second object (15)?

 

Please help!

THANK YOU IN ADVANCE!

-Jeff