jQuery AJAX简单的PHP Post无法正常工作

时间:2021-02-28 01:25:31

I've got a very basic one today! I'm trying to post from jQuery to a PHP file hosted on localhost.

我今天有一个非常基本的!我正试图从jQuery发布到localhost上托管的PHP文件。

My 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');
        }
  })
});

My PHP file (getNameInfo.php), a very basic one for testing:

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

<?php
  echo 'TEST';
?>

In jQuery, it will always bring me to error, saying 'Internal Server Error'. I am using Ripple Emulator and this is what appears in the console:

在jQuery中,它总是会让我犯错误,说“内部服务器错误”。我正在使用Ripple Emulator,这是控制台中出现的内容:

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

POST https://rippleapi.herokuapp.com/xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rurl=localhost%3A8080/getNameInfo.php 500(内部服务器错误)

I assume it has to do more with this than with what's written in the files. Any advice? Thanks!

我认为它必须做的更多,而不是文件中写的内容。任何建议?谢谢!

EDIT: Found this: LINK but it won't fix my issue. If I do what it says here, I won't get any error thrown ("" instead) but will still fail.

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

EDIT 2: If I set Cross-Domain Proxy to Local in Ripple (that's suggested in the link above) I get

编辑2:如果我在Ripple中设置跨域代理到本地(在上面的链接中建议)我得到

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

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

EDIT 3: Changed my URL to local

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

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

and it's working now. Have no idea how to make it work on localhost. BUt will go with this now, as it's only a learning app.

它现在正在运作。不知道如何使它在localhost上工作。 BUt现在将继续使用它,因为它只是一个学习应用程序。

2 个解决方案

#1


2  

Add a header access control at top of getNameInfo.php file :

在getNameInfo.php文件的顶部添加标头访问控制:

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

#2


0  

So, finally I found a solution. Even though it's not a recommended one, running Chrome with

所以,最后我找到了解决方案。即使它不是推荐的,也可以运行Chrome

--disable-web-security

will allow me to call a PHP script hosted on localhost:8080 from localhost:3000 where PhoneGap / Ripple Emulator stays.

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

#1


2  

Add a header access control at top of getNameInfo.php file :

在getNameInfo.php文件的顶部添加标头访问控制:

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

#2


0  

So, finally I found a solution. Even though it's not a recommended one, running Chrome with

所以,最后我找到了解决方案。即使它不是推荐的,也可以运行Chrome

--disable-web-security

will allow me to call a PHP script hosted on localhost:8080 from localhost:3000 where PhoneGap / Ripple Emulator stays.

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