1

I'm new to React js And I using Yii2 as my backend..! When I Send a API request to yii2 ,It Returns me the 500 Error.I don't know,Where I made a mistake.

Here is my React Js Code for API call,

fetch('localhost/learning-react/api/admin/signup', {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
          body: JSON.stringify({
            fname:fname,
            lname:lname,
            email:email,
            uname:uname,
            passwd:passwd
          })
        }).then((response) => response.json())
          .then((responseJson) => {
             if(responseJson['status']==='1') 
             {
              alert(responseJson['msg']);
             }
           }

And This is My Yii2 Backend code,

 public function actionSignup()
    {   
        //$model = new TblUsers();
        return "success";
    }

Sorry For my Inability to finding mistakes in my code..!

6
  • The 500 error is coming from the server, is that all the code you have? Commented Jun 22, 2018 at 12:44
  • Add CORS headers configuration to YII2 codebase specially for OPTIONS method. It may be a reason can you share the exact info you are execting the apis means the request method etc Commented Jun 22, 2018 at 13:26
  • look into the details of 500 error it's on the server side, are you able to send requests via ajax or curl to your API? Commented Jun 22, 2018 at 17:05
  • You can have more information on the error by using 1.xdebug or 2.use chrome open the debbuger click on the network tab, identify the file that is giving you the 500 error and look at the response so you can see the exact error that is being returned. Commented Jun 22, 2018 at 19:24
  • Now I really recommend you to start using Postman to check this APIs all the time. Commented Jun 22, 2018 at 21:07

2 Answers 2

2

First of all, 500 means generic server error. So you will need to post your error log as per my comment to help on that. However, I have found your code on the backend is wrong. You do not user restful controller in your code and as such it is not a REST API at all. So I advice you to read through Restful APIs in the Guide. That being said, you basically need to:

  1. Create Restful controller by inheriting from yii\rest\Controller.
  2. Return either Array, DataProvider or instance of yii\base\Model to have guarantee of automated serialization to JSON

so I will show you a simple example to give you an idea. Please read the guide to get in-depth insights on REST API with Yii2.

<?php

namespace app\modules\v1\controllers;

use yii\rest\Controller;

class LoginController extends Controller
{
    public function actionSignup()
    {
        $model = new TblUsers();
        //register a user
        //return registred user
        return [
            'success' => true,
            'member' => $model;
        ];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Let try the following, it may help.

You are making a rest post request, this means two things, first info will travel by POST request, and second dont forget its rest.

  1. try opening the url in the browser, unless you define a rule it should open.

So go ahead open: http://localhost/learning-react/api/admin/signup you should see a "success" on the screen, or you will se the full 500 error printed.

  1. If you were able to open the url on the browser, try the call again, and check your chrome debugger on the network tab. Look for the 500 error open it and read the error, it should be fully printed there on the response tab i.e. enter image description here

  2. when this is solved, don't forget to enable rules to allow only POST as request, and add the appropriate format for the response so you can consume it as json.

    Yii::$app->response->format = Response::FORMAT_JSON;

Hope it helps debuggin.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.