AJAX调用返回整个PHP文件

AJAX调用返回整个PHP文件

本文介绍了jQuery AJAX调用返回整个PHP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此感到困惑了几天,看了很多其他没有好运的帖子.

I've been stumped on this for a few days and have looked at a number of different posts without much luck.

我像这样使用jQuery进行AJAX调用:

I make an AJAX call using jQuery like so:

console.log("Sending post request");
  var postData = {
    "function": "connectToGame",
    "name" : name,
  };
  console.log(postData);
  $.ajax({
    type: "POST",
    url: 'http://localhost:8000/Cordova/server/database.php',
    data: postData,
    dataType: 'text',
    success: function(data)
    {
      console.log("AJAX Success! See return:");
      console.log(data);
      var id = data[0];              //get id

      console.log('Server returned: ' + id + ' as the id');
    },
    error: function(e){
      console.log("AJAX Failure!");
      console.log(e);
    }
  });

在删除了大量尝试缩小问题范围的代码之后,我的server/database.php当前如下所示:

After removing a lot of code trying to narrow down the problem, my server/database.php currently looks like this:

<?php
echo "1";
?>

调用AJAX后,控制台将显示以下内容:

After making the AJAX call the console shows this:

database.js:21 Sending post request
database.js:26 Object {function: "connectToGame", name: ""}
database.js:34 AJAX Success! See return:
database.js:35 <?php
echo "1";
exit;
?>

database.js:38 Server returned: < as the id

对于为什么AJAX调用会返回整个PHP文件,我深感困惑,不胜感激.

I am completely stumped as to why the AJAX call is returning the entire PHP file and any help would be greatly appreciated.

推荐答案

似乎您没有正确配置Web服务器来解释PHP文件,并且它不经过任何处理即可返回其文字内容.不幸的是,您没有提供有关服务器端设置的任何详细信息,因此很难提供帮助.但是,如果您使用的是Apache,则可以查看 mod_php ,以便您的PHP脚本由服务器评估,并将结果返回给客户端.因此,在尝试进行任何客户端调用之前,请确保在http://localhost:8000/Cordova/server/database.php处具有有效的服务器端脚本.

It looks like you haven't properly configured your webserver to interpret PHP files and it returns their literal contents without any processing. Unfortunately you haven't provided any details about your server side setup so it's pretty hard to help. But if you are using Apache you may take a look at mod_php so that your PHP script are evaluated by the server and the result is returned to the client. So make sure that you have a working server side script at http://localhost:8000/Cordova/server/database.php before trying to make any client side calls to it.

这篇关于jQuery AJAX调用返回整个PHP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:19