I am using php (to use MySQL) on an external server for Ajax communication in Monaca.
I would like to use php to display the contents of the database.I have done Ajax communication and received it via Json, but it doesn't work, so please advise me if there is anything on Monaca and php.
"Monaca side" execution result → js in console.log, error alert occurs.
// Press word of mouth button to perform page transition and update at the same time
function anime1() {
app.slidingMenu.setMainPage('page1.html', {closeMenu:true});
}
function anime2(){
console.log("jh");
// Access DB using ~.php with Ajax
$.ajax({
type: "POST",
scriptCharset: 'utf-8',
dataType: "jsonp",
url —The ~.php filename on the server.
}).done(function(data){
// Set the data pulled from DB to the option to select the dish name
console.log("Post Complete!");
// for (vari=0;i<data.length;i++){
// $('.food_name_box') .append($('<option>').html(data[i]["Japanese"]).val(data[i]["id"]));
// }
}).fail(function(data){
alert('error!!!');
// console.log("error");
});
}
"php side"
<?php
// DB connection
require_once('config.php');
// $text1 = $_POST ["sender_name" ];
// $text2 = $_POST ["sender_mail_address" ];
// $text3 = $_POST ["contact_body"];
// $text4 = $_POST ["time"];
$result=$pdo->prepare("select*from13DB066_test");
$result->execute();
$result_value=$result->fetchAll();
header('Content-type:application/json');
header('Content-type:html');
echo json_encode($result_value);
// var_dump($result_value);
Are you mistaken for JSON and JSONP?
http://tsujimotter.info/2013/01/03/jsonp/
dataType
specifies jsonp
, but the PHP output is just JSON.
Therefore, you should specify dataType: 'json'
, and you do not need scriptCharset
to specify the encoding for loading as a script.
Also, why have you run header('Content-type:...')
twice on the PHP side?I think the latter is extra (although html
is not the correct MediaType in the first place).
© 2024 OneMinuteCode. All rights reserved.