Laravel AJAX Form 405错误

时间:2016-02-02 05:16:53

标签: ajax laravel

尝试通过AJAX提交时收到此错误。当不使用AJAX时,表单提交就好了它的指定url。我已经读过这个错误,因为表单试图在浏览器中提交以及AJAX请求。我尝试过使用onsubmit =“event.preventDefault()”。

ROUTE:

Route::post('/post/{id}', [
    'uses' => '\App\Http\Controllers\PostController@postMessage',
    'as' => 'post.message',
    'middleware' => ['auth'],
]);

FORM:

<form id="post" role="form" action="{{ route('post.message', ['id' => $user->id]) }}" onsubmit="event.preventDefault()">
    <div class="feed-post form-group{{ $errors->has('post') ? ' has-error' : ''}}">
        <textarea class="form-control feed-post-input" rows="2" id="postbody" name="post" placeholder="What's up?"></textarea>
        <div class="btn-bar">
            <!-- <button type="button" class="btn btn-default btn-img btn-post" title="Attach an image"><span class="glyphicon glyphicon-picture"></span></button> -->
            <!-- <input type="file" id="img-upload" style="display:none"/> -->
            <button class="btn btn-default btn-post" title="Post your message"><span class="glyphicon glyphicon-ok"></span></button>
        </div>
        @if ($errors->has('post'))
            <span class="help-block">{{ $errors->first('post') }}</span>
        @endif
    </div>
    <input type="hidden" name="_token" value="{{ CSRF_token() }}">
</form>

控制器:

public function postMessage(Request $request, $id)
{

        $this->validate($request, [
            'post' => 'required|max:1000',
        ]);
  if(Request::ajax()){
         Auth::user()->posts()->create([
             'body' => $request->input('post'),
             'profile_id' => $id
         ]);
  }
}

JS:

$('#post').submit(function(){
    var body = $('#postbody').val();
    var profileId = $('#user_id').text();
    var postRoute = '/post/'+profileId;

    var dataString = "body="+body+"&profile_Id="+profileId;

    $.ajax({
        type: "POST",
        url: postRoute,
        data: dataString,
        success: function(data){
            console.log(data);
        }
    });
});

1 个答案:

答案 0 :(得分:0)

试试这个,删除

Gson gson = new GsonBuilder()
            .registerTypeAdapter(BillItem.class, new BillItemDeserializer())
            .registerTypeAdapter(Bills.class, new BillsDeserializer())
            .create();

RestAdapter.Builder builder = new RestAdapter.Builder()
            //...
            .setConverter(new GsonConverter(gson));

public class BillsDeserializer implements JsonDeserializer<StringList> {

    public Bills deserialize(JsonElement json, Type typeOfT,
            JsonDeserializationContext context) throws JsonParseException {
         BillItemList value = new BillItemList();
         if (json.isJsonArray()) {
             for (JsonElement element : json.getAsJsonArray()) {
                 value.add(gson.fromJson(element, BillItem.class));
             }
         } else {
             value.add(gson.fromJson(element, BillItem.class));
         }
         return value;
     }
}

并像这样更新您的提交方法

onsubmit="....." attribute from form

我希望这对你有用