我很确定我传递给register()
的所有参数都有值。这是js
代码。
$scope.register = function(
$reg_code, $prov_code, $citymun_code, $brgy_code, $street,
$user_name, $user_email, $user_contact, $user_password
) {
//displays the arguments
alert("region: " + $reg_code + ", province: " + $prov_code + ", citymun: " + $citymun_code + ", barangay: "
+ $brgy_code + ", street: " + $street + ", user_name: " + $user_name + ", user_email: " + $user_email + ", user_contact: "
+ $user_contact + ", user_password: " + $user_password);
$http({
method: "POST",
url: "http://" + host + "/mobile/register.php",
data: JSON.stringify({
user_password_reg: $user_password,
user_email_reg: $user_email,
user_contact_reg: $user_contact,
user_name_reg: $user_name,
region: $reg_code,
province: $prov_code,
citymun: $citymun_code,
barangay: $brgy_code,
street: $street,
echo: "1",
success: "0",
user_acc_type: "log account"
}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function(res) {
alert("success1: " + res.data.success1 + ", success2: " + res.data.success2 + ", success3: " + res.data.success3);
});
}
这是php脚本
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=utf-8');
//converts data to post
$postData = file_get_contents("php://input");
$post = json_decode($postData);
$echo = array();
$echo["success1"] = 0;
$echo["success2"] = 0;
$echo["success3"] = 0;
if(isset($post["region"]) &&
isset($post["province"]) &&
isset($post["citymun"]) &&
isset($post["barangay"]) &&
isset($post["street"]) &&
isset($post["user_password_reg"]) &&
isset($post["user_name_reg"]) &&
isset($post["user_email_reg"]) &&
isset($post["user_contact_reg"])
) {
$echo["success1"] = 1;
$echo["success2"] = 1;
$echo["success3"] = 1;
} else {
$echo["success1"] = 2;
$echo["success2"] = 2;
$echo["success3"] = 2;
}
echo json_encode($echo);
?>
此计划尚未完成。我首先要确保php从post发送所有正确的参数并返回或echo
是正确的值。但是,在这些代码中,我在控制台中收到此错误。
SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at fromJson (ionic.bundle.js:9892)
at defaultHttpResponseTransform (ionic.bundle.js:17406)
at ionic.bundle.js:17491
at forEach (ionic.bundle.js:9150)
at transformData (ionic.bundle.js:17490)
at transformResponse (ionic.bundle.js:18216)
at processQueue (ionic.bundle.js:22016)
at ionic.bundle.js:22032
at Scope.$eval (ionic.bundle.js:23228)
如果在php的json输出中读取<
,则表示php代码部分存在错误。由于AngularJS会自动解析来自php的数据,因此调试php代码非常困难。因此,我无法看到错误的细节。此外,我真的怀疑这个简单的PHP脚本会有错误。
提前致谢。
答案 0 :(得分:2)
您的PHP代码可能会触发导致HTML响应的错误。
这很可能是因为您尝试将对象属性作为数组索引访问,触发类似
的内容致命错误:无法使用stdClass类型的对象作为数组
将您的代码更改为
$post = json_decode($postData, true);
请参阅http://php.net/manual/function.json-decode.php#refsect1-function.json-decode-parameters
我还假设您使用Content-Type: application/x-www-form-urlencoded
来保留请求simple。我建议使用text/plain
,因为您的数据肯定不是网址编码。
$http.post("http://" + host + "/mobile/register.php", {
user_password_reg: $user_password,
user_email_reg: $user_email,
user_contact_reg: $user_contact,
user_name_reg: $user_name,
region: $reg_code,
province: $prov_code,
citymun: $citymun_code,
barangay: $brgy_code,
street: $street,
echo: "1",
success: "0",
user_acc_type: "log account"
}, {
headers: { 'Content-type': 'text/plain' }
})