Laravel API - JSONP 响应返回 500 内部服务器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35931453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:23:26  来源:igfitidea点击:

Laravel API - JSONP response returns 500 Internal Server Error

phpjsonajaxapilaravel

提问by LoveAndHappiness

I am working on a Laravel API. My web application is making a GETrequest to my API. But because the API is set to a subdomain (http://api.geladaris.dev/v1/books), I have to use JSONPto make Cross-Origin GETrequests work to this endpoint.

我正在研究 Laravel API。我的 Web 应用程序正在GET向我的 API发出请求。但是因为 API 设置为子域 ( http://api.geladaris.dev/v1/books),所以我必须使用JSONP使跨域GET请求工作到此端点。

On the client side I have a polymerelement, that generates the GETrequest and the server should return a jsonpresponse by Laravel.

在客户端,我有一个polymer元素,它生成GET请求,服务器应该jsonp通过 Laravel返回响应。

The Laravel documentation herestates, that the setCallbackmethod is to be chained to the response like this:

此处的 Laravel 文档指出,该setCallback方法将像这样链接到响应:

 return response()
        ->json(['name' => 'Abigail', 'state' => 'CA'])
        ->setCallback($request->input('callback'));

And that is what my indexmethod on my Api\BooksControllerlooks like:

这就是我的index方法的Api\BooksController样子:

public function index()
{
    return response()
        ->json(['name' => 'Abigail', 'state' => 'CA'])
        ->setCallback($request->input('callback'));
}

And my Routes.php

和我的 Routes.php

Route::group(['domain' => 'api.geladaris.dev'], function () {
    Route::group(['prefix' => 'v1'], function () {
        Route::resource('books', 'Api\BooksController');
    });
});

Unfortunately, all I get is a 500 Internal Server Error, when the request is generated and the server returns above code, with the "setCallback" method:

不幸的是500 Internal Server Error,当生成请求并且服务器使用“setCallback”方法返回上述代码时,我得到的只是一个:

500 Internal Server Error

500内部服务器错误

This only happens, if I use the setCallbackMethod. If I omit it, and return a normal jsonresponse instead of jsonp, the file is found, but I don't get the parseResponseI get, when accessing another API, f.e. http://api.github.com/usersor http://jsonplaceholder.typicode.com/posts. Notice the very first line:

这只会发生,如果我使用setCallback方法。如果我省略它,并返回一个正常的json响应而不是jsonp,找到文件,但我没有得到parseResponse我得到的,当访问另一个 API 时,fe http://api.github.com/usershttp:// jsonplaceholder.typicode.com/posts。注意第一行:

parseResponse in front of json

json 前面的 parseResponse

I am not sure how to proceed from here on. I have disabled the CSRFMiddleware and run my routes without any Middleware.

我不知道如何从这里开始。我已禁用CSRF中间件并在没有任何中间件的情况下运行我的路线。

I also tried using the apimiddleware, witout any positive results.

我也尝试使用api中间件,但没有任何积极的结果。

My next aproach would be to install a CORSlibrary and try setting the right response headers, but I doubt that the problem will be found there, since I checked and compared the headers from three different API's (Google News, Github, Json Placeholder) to my API and the only substantial difference I found, was the missing parseResponsebefore the actual returned JSON.

我的下一个方法是安装一个CORS库并尝试设置正确的响应标头,但我怀疑是否会在那里找到问题,因为我检查并将来自三个不同 API(Google 新闻、Github、Json Placeholder)的标头与我的API 和我发现的唯一实质性区别是parseResponse在实际返回的 JSON 之前丢失了。

Therefore I wonder: How do I set the JSONPresponse in Laravel to be found and not return 500 Server error?

因此我想知道:如何JSONP在 Laravel 中设置响应而不返回 500 服务器错误?



Since the error indicates a Server Error, here some details to the server:

由于错误表示服务器错误,这里有一些关于服务器的详细信息:

I am using WAMP.

我正在使用 WAMP。

My vhost:

我的vhost

<VirtualHost *:80>
    DocumentRoot "d:/wamp/www/geladaris/public"
    ServerName geladaris.dev
    ServerAlias *.geladaris.dev
</VirtualHost>

My hostsfile:

我的hosts文件:

127.0.0.1       geladaris.dev
127.0.0.1       api.geladaris.dev

回答by LoveAndHappiness

Turns out that

结果是

return response()
    ->json(['name' => 'Abigail', 'state' => 'CA'])
    ->setCallback($request->input('callback'));

Just required the Request to be put through the index method like this:

只需要像这样通过索引方法提交请求:

public function index(Request $request)
{
return response()
    ->json(['name' => 'Abigail', 'state' => 'CA'])
    ->setCallback($request->input('callback'));
}