我正在用jquery学习web api。
这是我发布帖子请求的插件:
$.postAPI = function (url, data) {
let defer = $.Deferred();
let onSuccess = function (data) {
defer.resolve(data);
};
let onError = function (error) {
defer.reject(error);
};
$.ajax({
url: url,
method: 'POST',
data: data || null
}).done(onSuccess).fail(onError);
return defer;
};
API控制器:
[Route("api/user")]
public class UserApiController : Controller
{
[HttpPost("{userid?}")]
public IActionResult GetData(string userid)
{
if (!string.IsNullOrEmpty(userid))
{
return Ok(userid);
}
return new StatusCodeResult(401);
// also try with
// return BadRequest();
// return Unauthorized();
}
}
测试:
$.postAPI('/api/user/getdata').done(function (data) {
console.log('success:', data);
}).fail(function (e) { console.log('fail:', e); });
但我总是得到这个日志:
成功:getdata
我想让请求变为fail
。因此,日志可能是:
失败:......
我的问题:我该怎么做?
更新
我尝试添加此行(根据评论):
Response.StatusCode = 404;
到方法。但问题没有解决。
答案 0 :(得分:1)
如果你调用“api / user”,这将直接调用你的函数“GetData”,因为你没有定义函数的路径。
url api / user / getdata 中的“getdata”部分将被解释为您的“用户ID”,因此1.0 = 0.0
将始终返回true。
如果你在没有“getdata”部分的情况下调用“api / user”,它应该会失败。
如果你想继续使用getdata作为url,你可以像这样为函数添加一个路由:
if (!string.IsNullOrEmpty(userid))
请参阅: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing
答案 1 :(得分:0)
试试这个:
<?php
$link = new mysqli('localhost','root','','foodbasketonline');
if($link->connect_error){
die("Connection Failed".$link->error);
}
$sql = "SELECT * FROM menus";
$result = $link->query($sql);
$row_count = $result->num_rows;
while($row = $result->fetch_assoc()){
$rows[] = $row;
}
?>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
#main_nav{
width:204px;
background:#f5f5f5;
padding:0;
}
.main_ul{
list-style:none;
padding:0;
position:relative;
margin-bottom:0;
}
li.main_li{
line-height:50px;
border-bottom:1px solid #ddd;
padding-left:20px;
}
ul.main_ul ul{
display:none;
}
ul.main_ul li:hover > ul{
display:block;
background:#fff;
border:1px solid #e5e5e5;
border-radius:5px;
width:150px;
line-height:40px;
padding-left:10px;
position:absolute;
}
ul.main_ul ul li:hover > ul{
display:block;
background:#fff;
border:1px solid #e5e5e5;
border-radius:5px;
width:150px;
margin-left:100px;
line-height:40px;
padding-left:10px;
position:absolute;
top:10px;
}
ul.main_ul ul ul li:hover > ul{
display:block;
background:#fff;
border:1px solid #e5e5e5;
border-radius:5px;
width:150px;
margin-left:80px;
line-height:40px;
padding-left:10px;
position:absolute;
top:30px;
}
@media only screen and (min-device-width: 320px) and (max-device-width: 320px) and (orientation: portrait) {
button.menu_btn{
position:relative;
left:-38%;
}
#main_nav{
width:100%;
background:#f5f5f5;
padding:0;
}
}
@media only screen and (min-device-width: 375px) and (max-device-width: 375px) and (orientation: portrait) {
button.menu_btn{
position:relative;
left:-38%;
}
#main_nav{
width:100%;
background:#f5f5f5;
padding:0;
}
}
@media only screen and (min-device-width: 414px) and (max-device-width: 414px) and (orientation: portrait) {
button.menu_btn{
position:relative;
left:-38%;
}
#main_nav{
width:100%;
background:#f5f5f5;
padding:0;
}
}
</style>
</head>
<body>
<button type="button" class="navbar-toggle btn btn-primary menu_btn" data-toggle="collapse" data-target="#main_nav">
<span class="icon-bar" style="background:#000;"></span>
<span class="icon-bar" style="background:#000;"></span>
<span class="icon-bar" style="background:#000;"></span>
</button>
<div class="collapse navbar-collapse" id="main_nav">
<?php
$items = $rows;
$id='';
echo '<ul class="main_ul">';
foreach($items as $item){
if($item['parent_id'] == 0){
echo '<li class="main_li"><a href="#">'.$item['menu'].'</a>';
$id = $item['id'];
sub($items, $id);
echo '</li>';
}
}
echo '</ul>';
function sub($items, $id){
echo '<ul style="list-style:none;">';
foreach($items as $item){
if($item['parent_id'] == $id){
echo '<li><a href="#">'.$item['menu'].'</a>';
sub($items, $item['id']);
echo '</li>';
}
}
echo '</ul>';
}
?>
</div>
</body>
</html>