duanjianxi8439 2018-12-06 10:21
浏览 97

Slim Framework中的Access-Control-Origin

Update this code in the index but I am unable to access this result method.

$app->add(function ($req, $res, $next) {
$response = $next($req, $res);
return $response
        ->withHeader('Access-Control-Allow-Origin', '*')
        ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
        ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');});


$app->post('/result', function () use ($app) {

$roll_no = $app->request->post('roll_no');  
$db = new DbOperation();
$response = array();
if ($db->result($roll_no)) {
    $student = $db->getStudentR($roll_no);
    $response['error'] = false;       
    $response['student_name'] = $student['student_name'];

} else{
    $response['error'] = true;
    $response['message'] = "Invalid username or password";
}
echoResponse(200, $response);});

Thank you. how can be accessed slim method?

  • 写回答

1条回答 默认 最新

  • douchan7552 2018-12-06 10:41
    关注

    you do not run your method. Also, you have to reteurn a response object:

    $app->post('/result', function (RequestInterface $request, ResponseInterface $response) use ($app) {
    
    $roll_no = $app->request->post('roll_no');  
    $db = new DbOperation();
    $result= array();
    if ($db->result($roll_no)) {
        $student = $db->getStudentR($roll_no);
        $result['error'] = false;       
        $result['student_name'] = $student['student_name'];
    
    } else{
        $result['error'] = true;
        $result['message'] = "Invalid username or password";
    }
    $response->getBody()->write(json_encode($result));
    return $response;});
    
    $app->run();
    

    you should then be able to run as follows:

    your-domain.com/result

    评论

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算