質問を見ていただきありがとうございます。
こちらの質問の派生です。前回の質問で正しく処理できない原因を教えていただきましたので、そこに注目して試行錯誤したのですが解決に至らずもう一度質問させていただきます。
以下ソースコードです。
###Javascript(jQuery)
場所:http://localhost:8888/sample/js/contact.js
$('#ContactButton').on('click', function(){ console.log('click'); var hostUrl= 'http://localhost:8888/sample/experiment/mail.php'; var data1 = "hogehoge"; var data2 = "nikuniku"; var array = {dataname1 : data1, dataname2 : data2}; console.log("JSON.stringify>>" + JSON.stringify(array)); $.ajax({ url: hostUrl, type:'POST', dataType: 'json', data : JSON.stringify(array), scriptCharset: 'utf-8', timeout:3000, }).done(function(data) { console.log("DONE"); }).fail(function(XMLHttpRequest, textStatus, errorThrown) { console.log("XMLHttpRequest.responseText>>" + XMLHttpRequest.responseText); console.log("XMLHttpRequest.status>>" + XMLHttpRequest.status); console.log("textStatus>>" + textStatus); console.log("errorThrown>>" + errorThrown); console.log("ERROR"); }) });
###Chrome consoleでの結果
click JSON.stringify>>{"dataname1":"hogehoge","dataname2":"nikuniku"} XMLHttpRequest.responseText>>Array ( [{"dataname1":"hogehoge","dataname2":"nikuniku"}] => ) <br>string(47) "{"dataname1":"hogehoge","dataname2":"nikuniku"}" XMLHttpRequest.status>>200 textStatus>>parsererror errorThrown>>SyntaxError: Unexpected token A in JSON at position 0 ERROR
###mail.php
<?php ini_set("allow_url_fopen", true); print_r($_POST); echo "<br>"; $json_string = file_get_contents('php://input'); var_dump($json_string); //検証のため$_POSTとfile_get_contents('php://input')で行いました。 ?> //表示結果 Array ( ) string(0) ""
###環境
OS:windows8.1
サーバー:Apache
PHP:Version 7.2.3
###自分なりの考察
XMLHttpRequest.responseText>>Array ( [{"dataname1":"hogehoge","dataname2":"nikuniku"}] => ) <br>string(47) "{"dataname1":"hogehoge","dataname2":"nikuniku"}"
とありますので
print_r($_POST); echo "<br>"; $json_string = file_get_contents('php://input'); var_dump($json_string);
PHPへの送信と処理は上手くいっているのかと思ったのですが、実際mail.phpを見てみるとなにも無いことになっており混乱しています。
不足している情報などが有りましたらできる限り提示いたします。回答よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/04/07 22:29
2018/04/07 22:30
退会済みユーザー
2018/04/07 22:30
退会済みユーザー
2018/04/07 22:33 編集
退会済みユーザー
2018/04/07 22:34
2018/04/07 22:35
2018/04/08 07:04