【发布时间】:2014-01-03 17:23:49
【问题描述】:
ajax 调用建立成功。但是当我尝试从 php 获得一些响应时,就会出现问题。在我拥有的 .php 文件中,<?php echo 'hello'; ?>。当我警告成功函数中的参数时,它会给我整个 php 文件。 ajax 调用中请求的数据类型是“文本”。请让我知道我在哪里犯了错误..
代码:
<script>
$(document).ready(function()
{
/* Attach a submit handler to the form */
//$("#foo").submit(function(event) {
$('form').on('submit', function(event){
/* Stop form from submitting normally */
event.preventDefault();
/* Clear result div*/
$("#result").html('');
/* Get some values from elements on the page: */
var values = $(this).serialize();
/* Send the data using post and put the results in a div */
$.ajax({
url: "ajaxSamplephp.php",
type: "post",
data: values,
datatype: "text",
success: function(data){
alert("success");
$("#result").html('Submitted successfully');
alert(data);
},
error:function(){
alert("failure");
$("#result").html('There is error while submit');
}
});
});
});
</script>
<body>
<form id="foo">
<label for="bar">A bar</label>
<input id="bar" name="bar" type="text" value="" />
<input type="submit" value="Send" />
</form>
<div id="result">RESULT</div>
</body>
PHP 文件
<?php
echo 'Hi I am some random';
?>
输出:<?echo 'Hi I am some random'; ?> 在警报窗口中
得到输出:我的 URL 是问题所在。我使用 wamp 服务器和 Eclipse PDT。工作区位于不同的位置,因此我无法获得输出。
在 php 文件中,我给出了 echo json_encode('[{"key":"value"}]'); 在 ajax 调用中,我将数据类型更改为 'json'。但是像alert(data.key)这样的警报;给我'undefined' - 消息。非常感谢您的帮助...
【问题讨论】:
-
echo '嗨,我是随机的';出口; ?
-
“它给了我整个 php 文件”:你想说什么(
-
退出;也被添加到警报消息中:(
-
当您直接导航到 ajaxSamplephp.php(使用您的浏览器)时会发生什么?您的网络服务器似乎无法解释 PHP。
-
文件下载窗口打开。并且下载的文件具有相同的输出内容...