I am having issues with reading Json_encode response in a java script.
我在java脚本中读取Json_encode响应时遇到问题。
The PHP file reads values from database and sends the results as Json_encode array to html.
PHP文件从数据库读取值并将结果作为Json_encode数组发送到html。
<?php
include("connect.php");
try {
$conn = new PDO("mysql:host=$servername;dbname=mydb", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "Call print_awb (@output1,@output2,:input_awb_ref_id)";
if (isset($_POST)) {
$p_in_awb_ref_id =reset($_POST["var_p_in_awb_ref_id"]);
}
$stmt = $conn->prepare($sql);
$stmt->bindParam(':input_awb_ref_id',$p_in_awb_ref_id, PDO::PARAM_INT);
$stmt->execute();
$out_awb_ref_id = $conn->query("SELECT @output1")->fetch(PDO::FETCH_ASSOC);
$out_agent_id = $conn->query("SELECT @output2")->fetch(PDO::FETCH_ASSOC);
$output = array(
"out_awb_ref_id" => $out_awb_ref_id,
"out_agent_id" => $out_agent_id,
);
echo json_encode($output);
$stmt->closeCursor();
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
?>
The HTML CODE
<script type="text/javascript">
function get_parameters(){
var quote = ABCD1234;
quote.toString();
window.alert(quote);
$.ajax({
type: "POST",
url: "printawb.php",
data: {var_p_in_awb_ref_id:quote},
dataType: "text",
success: function (result) {
alert(result);
var a = result.out_awb_ref_id;
alert (a);
alert(result['out_awb_ref_id']);
alert(result['out_agent_id'])
},
error:function (jqXHR, status, err){
//Fail
layer_1.html(html);
}
});
return vars;
};
</script>
The response I have is
HVP000062
{"out_awb_ref_id":{"@output1":"MIR"},"out_agent_id":{"@output2":"rtPreston"}} undefined undefined
{“out_awb_ref_id”:{“@ output1”:“MIR”},“out_agent_id”:{“@ output2”:“rtPreston”}} undefined undefined
All Need is the values in the 2nd line of the result ie. "MIR" and "rtPreston"
所有需要是结果的第二行中的值,即。 “MIR”和“rtPreston”
I have tried couple of things: 1. changed the calling function type as 'JSON' but the response was 'Object' without values 2. Tried converting to jsonString 3. tried reading values using JSON.parse(jsonString); 4. result['out_ref_awb_id']
我尝试了几件事:1。将调用函数类型更改为'JSON',但响应是'Object'而没有值2.尝试转换为jsonString 3.尝试使用JSON.parse(jsonString)读取值; 4.结果['out_ref_awb_id']
None of them work. Can someone help me how I can get values of these? I can then use them to populate on a html page.
他们都没有工作。有人可以帮助我如何获得这些价值观吗?然后我可以使用它们填充html页面。
Many thanks
1 个解决方案
#1
0
Add this as a first line in the HEAD section of your HTML template
将其添加为HTML模板的HEAD部分中的第一行
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
Then try this code:
然后尝试以下代码:
<script type="text/javascript">
function get_parameters(){
var quote = ABCD1234;
quote.toString();
window.alert(quote);
$.ajax({
type: "POST",
url: "printawb.php",
data: {var_p_in_awb_ref_id:quote},
dataType: "text",
success: function (result) {
var data = jQuery.parseJSON(result);
var param1 = data.out_agent_id;
var param2 = data.out_awb_ref_id;
alert(param1["@output2"]);
alert(param2["@output1"])
},
error:function (jqXHR, status, err){
//Fail
layer_1.html(html);
}
});
return vars;
};
</script>
#1
0
Add this as a first line in the HEAD section of your HTML template
将其添加为HTML模板的HEAD部分中的第一行
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
Then try this code:
然后尝试以下代码:
<script type="text/javascript">
function get_parameters(){
var quote = ABCD1234;
quote.toString();
window.alert(quote);
$.ajax({
type: "POST",
url: "printawb.php",
data: {var_p_in_awb_ref_id:quote},
dataType: "text",
success: function (result) {
var data = jQuery.parseJSON(result);
var param1 = data.out_agent_id;
var param2 = data.out_awb_ref_id;
alert(param1["@output2"]);
alert(param2["@output1"])
},
error:function (jqXHR, status, err){
//Fail
layer_1.html(html);
}
});
return vars;
};
</script>