javaスクリプトとPHPの間でデータのやり取りをしたいです。
html
1<!DOCTYPE html> 2<html> 3<head> 4 <meta charset="utf-8"> 5 <title>Ajax</title> 6 <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 7 <script> 8 $(function(){ 9 $("#button1").on('click', function(event){ 10 event.preventDefault(); 11 var param = { "text": "Hello" }; 12 13 $.ajax({ 14 type: "GET", 15 url: "get-jsonp.php", 16 data: param, 17 crossDomain: true, 18 dataType : "jsonp", 19 scriptCharset: 'utf-8' 20 }).done(function(data){ 21 alert(data.text); 22 }).fail(function(XMLHttpRequest, textStatus, errorThrown){ 23 alert(errorThrown); 24 }); 25 }); 26 }); 27</script> 28 29</head> 30<body> 31 <button id="button1">submit</button> 32</body> 33</html> 34
php
1<?php 2$text = filter_input(INPUT_GET, 'text'); 3$callback = filter_input(INPUT_GET, 'callback'); 4$callback = htmlspecialchars(strip_tags($callback)); 5 6$param = ['text' => $text . ", World!"]; 7 8header('Content-type: text/javascript; charset=utf-8'); 9printf("{$callback}(%s)", json_encode( $param )); 10
これで、ajax.htmlを開くと、buttonが出てきて、クリックしたら、HelloというTextにWorldがくっついたものが表示されるはずなのですが、できません。
どうすればよいでしょうか、よろしくお願い致します。
回答2件
あなたの回答
tips
プレビュー