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
Laravel API - JSONP response returns 500 Internal Server Error
提问by LoveAndHappiness
I am working on a Laravel API. My web application is making a GET
request to my API. But because the API is set to a subdomain (http://api.geladaris.dev/v1/books
), I have to use JSONP
to make Cross-Origin GET
requests 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 polymer
element, that generates the GET
request and the server should return a jsonp
response by Laravel.
在客户端,我有一个polymer
元素,它生成GET
请求,服务器应该jsonp
通过 Laravel返回响应。
The Laravel documentation herestates, that the setCallback
method 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 index
method on my Api\BooksController
looks 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”方法返回上述代码时,我得到的只是一个:
This only happens, if I use the setCallback
Method. If I omit it, and return a normal json
response instead of jsonp
, the file is found, but I don't get the parseResponse
I 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/users或http:// jsonplaceholder.typicode.com/posts。注意第一行:
I am not sure how to proceed from here on. I have disabled the CSRF
Middleware and run my routes without any Middleware.
我不知道如何从这里开始。我已禁用CSRF
中间件并在没有任何中间件的情况下运行我的路线。
I also tried using the api
middleware, witout any positive results.
我也尝试使用api
中间件,但没有任何积极的结果。
My next aproach would be to install a CORS
library 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 parseResponse
before the actual returned JSON.
我的下一个方法是安装一个CORS
库并尝试设置正确的响应标头,但我怀疑是否会在那里找到问题,因为我检查并将来自三个不同 API(Google 新闻、Github、Json Placeholder)的标头与我的API 和我发现的唯一实质性区别是parseResponse
在实际返回的 JSON 之前丢失了。
Therefore I wonder: How do I set the JSONP
response 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 hosts
file:
我的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'));
}