javascript - How to connect frontend (js) and backend (php) in a local application? - Stack Overflow

So confused about how to connect frontend and backend? Suppose I've got an object obj and var json

So confused about how to connect frontend and backend?

Suppose I've got an object obj and var jsonText = JSON.stringify(obj);

how can I send this jsonText to backend (locally, non remote server, no database), using php to get this data and then save the content as a single new JSON file?

Thanks a lot!

So confused about how to connect frontend and backend?

Suppose I've got an object obj and var jsonText = JSON.stringify(obj);

how can I send this jsonText to backend (locally, non remote server, no database), using php to get this data and then save the content as a single new JSON file?

Thanks a lot!

Share asked Dec 30, 2015 at 23:07 KAFFEECKOKAFFEECKO 1731 gold badge2 silver badges10 bronze badges 9
  • Ajax. – Script47 Commented Dec 30, 2015 at 23:07
  • @Script47 but how to define the post target address? – KAFFEECKO Commented Dec 30, 2015 at 23:10
  • Using the url parameter which is available. – Script47 Commented Dec 30, 2015 at 23:11
  • Check this page to see how to run local php application. – Ahamed Commented Dec 30, 2015 at 23:13
  • 1 it would be: /your-file.php which is located on http://localhost/ .. the full path might be something like http://localhost/your-file.php and as @Ahamed posted, make sure you have a web server running otherwise what I said is not possible. – Clay Commented Dec 30, 2015 at 23:14
 |  Show 4 more ments

1 Answer 1

Reset to default 3

You need to query your data from a database or from somewhere else in PHP. Then, you can echo it with PHP in a JSON format. In a second step, you can use jQuery or plain JavaScript to make an Ajax call to this PHP file and make something with it.

PHP (data.json.php):

<?php
  header('Content-Type: application/json');
  $output = array();
  // query the data from somewhere
  $output['data'] = "1234";
  echo json_encode($output); // json_encode creates the json-specific formatting
?>

JavaScript (jQuery):

$.ajax({
   url: "data.json.php",
   success: function(result){
      console.log(result);
   }
});

The code is untested, but I think you should get the idea.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744861115a4597707.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信