我使用authorize标签创建了一个简单的webapi控制器,如下所示。当我未经过身份验证时,我用jquery调用它,并在响应中收到消息
public class TenantWebApiController : ApiController
{
// GET: api/TenantWebApi
[Authorize]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
...
但是这会带来200次成功,这意味着将调用jquery错误处理程序成功。
使用WebAPI 2,我希望在未经授权时抛出403,而不是200.我怎样才能实现此目的?
控制器文件:
$(document).ready(function () {
$.ajax({
url: '/api/TenantWebApi',
type: 'GET',
dataType: 'json',
data: {
firstName: 'Peter',
lastName: 'Kellner'
},
error: function(jqXHR, textStatus, errorThrown) {
alert('An error occurred');
},
success: function(data, textStatus, jqXhr) {
alert('success');
}
});
});
Index.cshtml
<?php
namespace App\Http\Controllers;
use App\GanttTask;
use App\GanttLink;
use Dhtmlx\Connector\GanttConnector;
class GanttController extends Controller
{
public function data(Request $request, $project_id) {
$connector = new GanttConnector(null, "PHPLaravel");
$connector->render_links(new GanttLink(), "id", "source,target,type");
// Passing as argument project_id variable to model constructor
// new GanttTask(compact('project_id')) will create object
// but will not save to database,
// so You can use:
// GanttTask::create(compact('project_id')) to push to db
$connector->render_table(new GanttTask(compact('project_id')),"id","start_date,duration,text,progress,parent,project_id");
}
}