jQuery AJAX 简单 PHP Post 不工作

jQuery AJAX simple PHP Post not working

我今天有一个非常基本的!我正在尝试 post 从 jQuery 到本地主机上托管的 PHP 文件。

我的 JS:

$("#searchNameButton").click(function() {
var name = $("#searchNameText").val();
 $.ajax({
        type: 'POST',
        url: 'localhost:8080/getNameInfo.php', // -> this works fine from the browser
        // data: { name: name }, -> commented out
        success: function(){
            alert('request successful');
        },
        error: function(xhr, textStatus, errorThrown){
            alert('request failed');
        }
  })
});

我的PHP文件(getNameInfo.php),一个非常基本的测试文件:

<?php
  echo 'TEST';
?>

在jQuery中,总是让我出错,说'Internal Server Error'。 我正在使用 Ripple 模拟器,这是控制台中显示的内容:

POST https://rippleapi.herokuapp.com/xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rurl=localhost%3A8080/getNameInfo.php 500 (Internal Server Error)

我认为与文件中所写的内容相比,它必须为此做更多的事情。有什么建议吗?谢谢!

编辑: 发现:LINK 但它不能解决我的问题。如果我按照这里所说的去做,我不会抛出任何错误(而是“”),但仍然会失败。

编辑 2: 如果我在 Ripple 中将跨域代理设置为本地(在上面 link 中建议),我得到

OPTIONS http://localhost:4400/ripple/xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rurl=http%3A//localhost%3A8080/getNameInfo.php net::ERR_CONNECTION_REFUSED

编辑 3:将我的 URL 更改为本地

C://Mobile//Cross-Platform//TestApp//www//php//getNameInfo.php

现在可以使用了。不知道如何让它在本地主机上工作。但是现在就用这个​​,因为它只是一个学习应用程序。

所以,我终于找到了解决办法。 尽管这不是推荐的,运行 Chrome 和

--disable-web-security

将允许我从 localhost:3000 调用托管在 localhost:8080 上的 PHP 脚本,其中 PhoneGap / Ripple模拟器保留。

getNameInfo.php 文件顶部添加 header 访问控制:

header('Access-Control-Allow-Origin: *');