带有ajax的Laravel 5.5显示500内部服务器错误

时间:2018-05-19 12:38:52

标签: jquery ajax laravel laravel-5 csrf

我发送了一条Ajax调用,显示错误消息:

  

500内部服务器错误

我已经浏览了Stackoverflow上的一些帖子并了解我必须传递csrf令牌。因此,我已将以下代码粘贴在head部分中(就在关闭head标记之前)。

 int questionsCounter = 5;
    enum Answers { CORRECT, INCORRECT };

    struct Student {
        int _stNumber;
        char _name[30];
        int _year;
        Answers *_answers;
        char * _userName;
        char *_password;
    };

    struct AllAnswers {
        int AnswerNumber;
        char *Question;
        Answers correctAnswer;
    };

    void dataEntry(Student * allStudents, int max) {

    for (int i = 0; i<max; i++) {
        cout << "\t::STUDENT " << i + 1 << "::";
        cout << "Enter Student's name: ";
        cin.getline(allStudents[i]._name, 30);
        cout << "Enter Student's number: ";
        cin >> allStudents[i]._stNumber;
        cout << "Enter Student's year (1,2,3,4): ";
        cin >> allStudents[i]._year;


        allStudents[i]._userName = new char[11];
        allStudents[i]._userName = GetUserName(allStudents[i]);

        allStudents[i]._password = nullptr;
        changePassword(allStudents[i]);     
    }
}

void addNewAnswer (AllAnswers *& oldAnswers, AllAnswers newAnswer) {

    AllAnswers *temp = new AllAnswers[questionsCounter+1];


    for (int i = 0; i < questionsCounter; i++)
    {   
        copyAnswer(oldAnswers[i], temp[i]);
    }

    copyAnswer(newAnswer, temp[questionsCounter]);

    deallocateAnswers(oldAnswers);

    assert(oldAnswers != NULL);

    oldAnswers = new AllAnswers[questionsCounter+1];


    for (int i = 0; i < questionsCounter+1; i++)
    {
        copyAnswer(temp[i], oldAnswers[i]); 

    }

    questionsCounter++; 
}

此外,这里是我调用的Ajax代码;

<meta name="csrf_token" content="{{ csrf_token() }}">

从上面可以看出,我还传递了带标题的令牌。还有什么我需要做的吗?

我的路线(web.php)如下:

$.ajax({
                        headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                        },
                        url: "adminUser/exportselected/1",
                        type:'POST',
                        data: {selected:selected, selectedField:selectedField},
                        success: function(data) { 
                            window.location = data.path;
                            if($.isEmptyObject(data.error)){
                                //alert(data);
                            }else{
                                //alert(data.error);
                            }
                        }
                    });

以下是我的控制器代码;

public function exportSelected($ page,Request $ request){

Route::post('adminUser/exportselected/{page?}', 'Administrator\AdminUserController@exportSelected');

2 个答案:

答案 0 :(得分:0)

你应该将dataType添加到你的ajax中,并将你的ajax代码中的标题移出函数,这里是示例代码

typedef struct

Ps:我用电话我的代码可能有点乱,对不起。

答案 1 :(得分:0)

实际上,您应该检查浏览器控制台,了解服务器中发生了什么。 laravel csrfTokenMissMatch抛出Laravel 419 post error而不是500 code。因此,请检查您的控制器exportSelected()方法,了解其错误。 您已成功验证csrf。 500错误来自控制器方法,而不是来自laravel csrf中间件。