前提・実現したいこと
ajaxで渡したデータをPHP側でechoで表示がしたいです。
もしくはdoneにてconsole.logで確認したいです。
発生している問題・エラーメッセージ
一部分ですが、下記のようなHTMLが返ってきます。
html
1<!doctype html> 2<html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 6 <!-- Always force latest IE rendering engine or request Chrome Frame --> 7 <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"> 8 <meta name="viewport" content="width=device-width, initial-scale=1.0" /> 9 10 <!-- Use title if it's in the page YAML frontmatter --> 11 <title>Welcome to XAMPP</title> 12 13 <meta name="description" content="XAMPP is an easy to install Apache distribution containing MariaDB, PHP and Perl." /> 14 <meta name="keywords" content="xampp, apache, php, perl, mariadb, open source distribution" /> 15 16 <link href="/dashboard/stylesheets/normalize.css" rel="stylesheet" type="text/css" /><link href="/dashboard/stylesheets/all.css" rel="stylesheet" type="text/css" /> 17 <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/3.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" /> 18 19 <script src="/dashboard/javascripts/modernizr.js" type="text/javascript"></script> 20 21 22 <link href="/dashboard/images/favicon.png" rel="icon" type="image/png" /> 23
該当のソースコード
js
1 // POSTでアップロード 2 $.ajax({ 3 url : "../index.php", 4 type : "POST", 5 data : { name : "hoge" }, 6 }) 7 .done(function(response, textStatus, xhr){ 8 console.log(response); 9 }) 10 .fail(function(xhr, textStatus, errorThrown){ 11 alert("fail"); 12 });
php
1<?php 2header('Access-Control-Allow-Origin: *'); 3header("Content-Type: application/javascript; charset=UTF-8"); 4 5$post_data_2 = $_POST['name']; 6echo json_encode($post_data_2);
試したこと
そもそもPHPでechoをして表示というのはhtml側で呼んでいるわけではないので認識が少し違うのではと思っています。そのためdone内で確認出来るのかと思ったのですが想定の値ではなく大量のhtmlが返ってきます。
###環境
windows10
xampp
回答1件
あなたの回答
tips
プレビュー