Javascript Ajax Slim框架
我正在开发一个ajax来调用slimframework API并将数据发布到DB。但是ajax不起作用吗?我在阿贾克斯的成绩一直是404 这很有效 在Ajax中尝试时,这是行不通的:发送数据时返回404,就好像您访问API时没有发送put请求一样Javascript Ajax Slim框架,javascript,php,jquery,ajax,slim,Javascript,Php,Jquery,Ajax,Slim,我正在开发一个ajax来调用slimframework API并将数据发布到DB。但是ajax不起作用吗?我在阿贾克斯的成绩一直是404 这很有效 在Ajax中尝试时,这是行不通的:发送数据时返回404,就好像您访问API时没有发送put请求一样 <script type="text/javascript"> var Data = [ { "Name" : 'Home', "Description" : "This is a test desc
<script type="text/javascript">
var Data = [
{
"Name" : 'Home',
"Description" : "This is a test description",
"Keywords" : "Keyword One, Two, Threeh",
"Menu" : 1,
"Parent" : 0
},
{
"Name" : 'About',
"Description" : "This is a About description",
"Keywords" : "Keyword One, Two, Threeh",
"Menu" : 2,
"Parent" : 0
}
];
var rootURL = "http://myapi.com/api/key/514315/push";
$.each(Data, function(key, value) {
$.ajax({
type: 'POST',
contentType: 'application/json',
url: rootURL,
dataType: "json",
data: Data[key],
success: function(data, textStatus, jqXHR){
alert('Article Added');
},
error: function(jqXHR, textStatus, errorThrown){
alert('Add Article error: ' + textStatus);
}
});
});
PHP
有什么想法吗
将其添加到PHP中typedefault:'GET'类型:String发出POST或GET请求的类型,默认为GET。注意:这里也可以使用其他HTTP请求方法,如PUT和DELETE,但并非所有浏览器都支持它们。这仍然显示404?
<script type="text/javascript">
var Data = [
{
"Name" : 'Home',
"Description" : "This is a test description",
"Keywords" : "Keyword One, Two, Threeh",
"Menu" : 1,
"Parent" : 0
},
{
"Name" : 'About',
"Description" : "This is a About description",
"Keywords" : "Keyword One, Two, Threeh",
"Menu" : 2,
"Parent" : 0
}
];
var rootURL = "http://myapi.com/api/key/514315/push";
$.each(Data, function(key, value) {
$.ajax({
type: 'POST',
contentType: 'application/json',
url: rootURL,
dataType: "json",
data: Data[key],
success: function(data, textStatus, jqXHR){
alert('Article Added');
},
error: function(jqXHR, textStatus, errorThrown){
alert('Add Article error: ' + textStatus);
}
});
});
$app->post('/key/:key/push', function ($key) {
$app = new \Slim\Slim();
$body = $app->request->getBody();
$singleRequest = $app->request->params('test');
echo "<pre>";
print_r($body);
echo "</pre>";
echo "<hr>";
echo "<pre>";
print_r($singleRequest);
echo "</pre>";
});
$app->run();
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
if(array_key_exists('HTTP_ACCESS_CONTROL_REQUEST_HEADERS', $_SERVER)) {
header('Access-Control-Allow-Headers: '
. $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
} else {
header('Access-Control-Allow-Headers: *');
}
if("OPTIONS" == $_SERVER['REQUEST_METHOD']) {
exit(0);
}