简体   繁体   中英

How Can I put a php response into i javascript variable?

I have a .php document on my Server. This sents me a JSON string, if I call it in browser. Now I want to put this reponse I get in browser into a JavaScript variable. Does anyone know how to do this?

MFG Jochen

What you are looking for is XMLHttpRequest , which enables to initiate HTTP requests from JavaScript code; don't pay attention to the misleading XML in the name.

All modern JavaScript libraries offer high-level functions to enhance the features of this function (eg jQuery.ajax ).

Try

  // 't' being the JS variable
   var t = <?php echo $json_string; ?>; 

You may want to use json_decode if you want the decoded string.

var response = <?php echo $json_echo; ?>;

this way you can pass php variable to JS

<script> 
     var json_js = <?php echo $json_php; ?>; 
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM