我正在使用PHP RestServer Class练习。但是如果我对它使用 Ajax 调用,我无法从中获取正确的数据。我有以下代码:
<?php
require_once "locationOfRestServer.php";
class HelloWorld
{
public static function sayHello()
{
return array("Response" => "Hello World");
}
}
$rest = new RestServer('HelloWorld');
$rest->handle();
在我的javascript文件中,我使用了以下内容:
this.helloWorld = function() {
$.ajax({
url: 'locationOfHelloWorld.php'
type: 'POST',
dataType: 'json',
success: function(data){
console.log(data);
}
});
};
我收到以下错误:
错误:"未请求任何方法。
因为;每当我使用它时,我都必须去localhost/HelloWorld.php?method=sayHello
它实际上是有效的。所以我在 ajax 调用中添加了以下行:
方法:"说你好",
但它仍然不断给我同样的错误。
试试这个,
$.ajax({
url: 'locationOfHelloWorld.php'
type: 'GET',// use GET method according to your working url
data:{method: 'sayHello'},// use method in data parameter
dataType: 'json',
success: function(data){
console.log(data);
}
});