質問を見ていただきありがとうございます。できる限り詳しく載せましたが、足りない場合は教えていただけると嬉しいです。
困っていること
- Ajaxで送ったJsonがmb_convert_encodingで変換できません。そもそもそこが問題なのかが怪しいのですが...
- また質問の主旨とはずれますが、XMLHttpRequest.responseTextの結果がよく理解できませんでした。ついでにご教授いただけますと幸いです。
下記の[Chromeコンソールの出力結果]の"XMLHttpRequest.responseText>>..."はXMLHttpRequestレスポンス(サーバーから帰ってくるもの?)だと解釈したのですが、なぜPHPでエラーとなっているのにレスポンスが帰ってくるのか不思議です。
以下、ソースコードです。
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コンソールの出力結果
click JSON.stringify>>{"dataname1":"hogehoge","dataname2":"nikuniku"} XMLHttpRequest.responseText>>string(47) "{"dataname1":"hogehoge","dataname2":"nikuniku"}" json_charset>> ASCII<br>object(stdClass)#1 (2) { ["dataname1"]=> string(8) "hogehoge" ["dataname2"]=> string(8) "nikuniku" } json_last_error>> 0 XMLHttpRequest.status>>200 textStatus>>parsererror errorThrown>>SyntaxError: Unexpected token s in JSON at position 0 ERROR
Mail.php
場所:http://localhost:8888/sample/experiment/mail.php
<?php $json_string = file_get_contents('php://input'); var_dump($json_string); echo "<br>"; mb_language("Japanese"); $json_string = mb_convert_encoding($json_string, "UTF-8", "ASCII,JIS,UTF-8,EUC-JP,SJIS"); echo "json_charset>> ".mb_detect_encoding($json_string)."<br>"; $json_array = json_decode($json_string); var_dump($json_array); echo 'json_last_error>> '.json_last_error(); ?>
Mail.phpの表示結果
string(0) "" json_charset>> ASCII NULL json_last_error>> 4
環境
OS:windows8.1
サーバー:Apache
PHP:Version 7.2.3
しばらくこのエラーに悩まされています...
回答よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー