我已经在Laravel中构建了一个REST API,它目前正在使用MAMP在本地运行,尽管我已经在实时服务器上试用了它并且同样的问题也出现了。
当我尝试将我的表单发布到API时它不起作用,除非我同时让Artisan在不同的端口号上为该站点提供服务并发布到该URL,即www.mysite.com:2834/api/courses,I可以在浏览器中获取页面没问题,这只是POST'ing导致问题。
我已经启用了CORs并且我尝试使用PostMan执行相同的POST请求并且它提供了相同的结果,但是当它只是我发布的正常URL www.mysite.com/api/courses但是工作时不起作用当我在不同的港口运行Artisan并发布到www.mysite.com:2834/api/courses。
奇怪的是,当使用CURL完成时,POST没有Artisan运行:
curl -i -H "X-Requested-With: XMLHttpRequest" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://www.mysite.io/api/courses --data "&author=tertertert&name=ertertert&description=ertertret&price=ertretret"
这是我的路线列表:
| | GET|HEAD | api/courses | api.courses.index | App\Http\Controllers\CourseController@index | web,cors |
| | POST | api/courses | api.courses.store | App\Http\Controllers\CourseController@store | web,cors |
| | GET|HEAD | api/courses/{courses} | api.courses.show | App\Http\Controllers\CourseController@show | web,cors |
| | PUT|PATCH | api/courses/{courses} | api.courses.update | App\Http\Controllers\CourseController@update | web,cors |
| | DELETE | api/courses/{courses} | api.courses.destroy | App\Http\Controllers\CourseController@destroy | web,cors |
和我的控制员:
<?php
namespace App\Http\Controllers;
use App\Course;
use App\Http\Requests;
use App\Http\Requests\CourseForm;
class CourseController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$courses = Course::all();
return response()->json(['courses' => $courses]);
}
/**
* @param CourseForm $request
* @return \Illuminate\Http\JsonResponse
*/
public function store(CourseForm $request)
{
$course = new Course();
$course->author = $request->input('author');
$course->name = $request->input('name');
$course->description = $request->input('description');
$course->price = $request->input('price');
$course->save();
return response()->json(['message' => 'Course saved']);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$course = Course::find($id);
return response()->json(['course' => $course]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(CourseForm $request, $id)
{
if($request->ajax())
{
$course = Course::find($id);
$course->author = $request->input('author');
$course->name = $request->input('name');
$course->description = $request->input('description');
$course->price = $request->input('price');
$course->save();
return response()->json(['message' => 'Course updated']);
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$course = Course::find($id);
$course->delete();
return response()->json(['message' => 'Course removed']);
}
}
和我的路线档案:
<?php
Route::group(['prefix' => 'api', 'middleware' => ['cors']], function(){
Route::resource('courses', 'CourseController', ['except' => [
'create', 'edit'
]]);
});
Route::group(['prefix' => 'api', 'middleware' => ['cors']], function(){
Route::resource('test', 'HomepageController@index');
Route::resource('projects', 'ProjectController@index');
});
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
Route::group(['before' => 'auth', 'middleware' => ['auth']], function() {
Route::get('/admin/','DashboardController@overview');
});
//Route::get('/test', 'HomepageController@index');
答案 0 :(得分:0)
女士们,先生们真实的掌心方式我已经弄清楚到底出了什么问题......在网址末尾的尾随斜线正在搞乱这一切......