I want to convert php (display of contents obtained from DB) by Json and display it on the screen by javascript.

Asked 1 years ago, Updated 1 years ago, 81 views

Monaca has completed console.log ("Post Completed") but
After-processing, I didn't understand the part of the JSON converted php sentence (displaying the contents of DB) on the Monaca screen in Javascript, so please give me some advice.

Monaca side

// 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){
                  console.log("Post Complete!");


                   // for (vari=0;i<data.length;i++)

                   // document.write$(':text[name="senderName"]')
                   //.append($('<option>') .html(data[i]["Japanese"]).val(data[i]["id"]));
         // }

                   }).fail(function(data){
                   alert('error!!!');
                   console.log("error");
                   });
            }


            </script> 

php side

<?php

          require_once('config.php');

          $result=$pdo->prepare("select*from13DB066_test");
          $result->execute();
          $result_value=$result->fetchAll();

          header('Content-type:application/json');

          echo json_encode($result_value);


          // echo$result_value[$i]["text1"];
          // echo$result_value[$i]["text2"];
          // echo$result_value[$i]["text3"];
          // echo$result_value[$i]["text4"];

          // }
          ?>

javascript php monaca json

2022-09-30 21:12

1 Answers

If it is an external server and not CORS, it will be JSONP (requires P).

$.ajax({
    type: "POST",
    scriptCharset: 'utf-8',
    dataType: "jsonp",
    url: 'http://example.com/foo.php?callback=?'
}).done(function(data){

This way, the php side is

<?php
require_once('config.php');

$result=$pdo->prepare("select*from13DB066_test");
$result->execute();
$result_value=$result->fetchAll();

header('Content-type:application/json');

$callback = $_GET ['callback'];
if($callback){
  // Function name check required
  echo$callback+'('+json_encode($result_value)+')';
} else{
  echo json_encode($result_value);
}

// echo$result_value[$i]["text1"];
// echo$result_value[$i]["text2"];
// echo$result_value[$i]["text3"];
// echo$result_value[$i]["text4"];

// }
?>

I will be able to return it with callback like this.The ? of callback=? in jQuery contains a unique function name, so php must return it as a function call.
Of course, you should check if the name is acceptable as a function of javascript.


2022-09-30 21:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.