json发送到服务器php文件作为来自ibm worklight Http适配器的发布请求即使解码后也无法访问:
以下是代码:
Http适配器:
function storeRegistrationData(emailVal, passVal, fname1, gender, phone, userType, vehType) {
var credentials = JSON.stringify({jemailVal: emailVal, jpassVal: passVal, jfname1: fname1, jgender: gender, jphone: phone, juserType : userType, jvehType : vehType});
var input = {
method : 'post',
returnedContentType : 'json',
path : "/carbikepooling/index.php",
parameters: {credentials: credentials}
};
return WL.Server.invokeHttp(input);
}
服务器php代码:
$jsonObj = (isset($_POST['credentials']) ? $_POST['credentials'] : null);
$credentials = json_decode($jsonObj);
$email = $credentials->jemailVal;
$pass = $credentials->jpassVal;
$uname = $credentials->jfname1;
$gender = $credentials->jgender;
$phone = $credentials->jphone;
$usertype = $credentials->juserType;
$vehtype = $credentials->jvehType;
$boolean = false;
$userId; $userTypeId;
// sanitation, database calls, etc
$connection = mysqli_connect("localhost","root","","carbikepooling");
if (mysqli_connect_errno($connection))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($connection,"insert into userInfo values(0,".$email.",'".$pass."','".$uname."','".$gender."','".$phone."')");
$result1 = mysqli_query($connection, "select u.userId from userInfo u order by u.userId desc limit 1");
if($result1){
$row1 = mysqli_fetch_row($result1);
$userId = $row1[0];
mysqli_query($connection,"insert into userType values(0,".$userId.",'".$usertype."')");
$result2 = mysqli_query($connection, "select t.userTypeId from userType t order by t.userTypeId desc limit 1");
if($result2){
$row2 = mysqli_fetch_row($result2);
$userTypeId = $row2[0];
mysqli_query($connection, "insert into vehicleType values(0,".$userTypeId.",'".$vehtype."')");
$boolean = true;
}
}
mysqli_close($connection);
$returnData[] = array(
'boolean' => "$boolean"
);
echo json_encode($returnData);
?>
显示来自服务器php的返回结果的Javascript代码:
function storeFormData(emailVal, passVal, fname1, gender, phone, userType, vehType){
var invocationData = {
adapter : 'registerUser',
procedure : 'storeRegistrationData',
parameters : [emailVal, passVal, fname1, gender, phone, userType, vehType]
};
WL.Client.invokeProcedure(invocationData, {
onSuccess : storeFormDataSuccess,
onFailure : storeFormDataFailure,
});
}
function storeFormDataSuccess(result){
WL.Logger.debug('Data stored Successfully');
alert(JSON.stringify(result.invocationResult));
}
function storeFormDataFailure(result){
WL.Logger.error('Error in storing Data');
}
当我在insert语句中使用服务器php文件中的解码json数据时,没有任何反应。我尝试了电子邮件,性别等的回显值,但没有打印出来,好像它们不包含任何值。但是,如果我在returnData数组中发送了电子邮件,性别等的已解码json值,因为它们在insert语句中使用,则由发出请求的应用程序成功接收的值,即:在js代码中的警报中显示这些值。
请解决这个问题?
答案 0 :(得分:0)
你能改变你的mysql查询看起来更像这样吗?
mysqli_query($connection, "insert into vehicleType values(0, '$userTypeId','$vehtype')");
请注意我是如何删除连接的'。'从查询和我现在我只是用单引号包围变量。