donglu1973 2016-11-15 15:47 采纳率: 100%
浏览 33
已采纳

带有ajax json的Laravel应用程序返回发出错误

My question is I have my page submitted however what I'm trying to do is have it submit back and display any errors that might have been from the validation underneath the proper form field. As of right now I am also getting this error in my console.

If you are willing to look through the little bit of code see if there's anythign suggested for a change to improve upon it.

"NetworkError: 405 Method Not Allowed - http://myapp.app/createorderfromform"
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

var err = JSON.parse(xhr.responseText);



$("#pageOneSubmission").click(function(event) {
    event.preventDefault();
    var email = $("input[name=email]").val();
    var addons = [];
    $('.addon-checkbox:checked').each(function() {
        addons.push( $(this).val() )
    });

    $.ajax({
        type: "POST",
        url: '{{action('OrderProcessController@createUserAndOrder') }}',
        data:
            {
                email: email,
                addons: addons
            },
        dataType: JSON,
        success: function(data) {
            if (!data.success) {
                console.log("Errors");
            } else {
                console.log("No Errors");
                $("#user-created-confirmation").html(data);
            }
        },
        error: function(xhr, status, error) {
            var err = JSON.parse(xhr.responseText);
            $.each(err, function(key, value) {
                $('input [name=key]').next().append("<p>Test</p>");
            });
        }
    }, function(){
        setTimeout(function() {

        })
    });
});

OrderProcessController

<?php

namespace App\Http\Controllers;

use App\Order;
use Illuminate\Http\Request;
use Sentinel;


class OrderProcessController extends JoshController
{
   /*
    * Create User After they complete the first part of the form. 
    *
    */
    public function createUserAndOrder(Request $request)
    {
        $validation = $this->validate($request, [
            'email' => 'required|confirmed|unique:users,email',
        ]);

        $credentials = [
            'email'    => $request->input('email'),
            'password' => $request->input('password'),
        ];

        $user = Sentinel::registerAndActivate($credentials);

        $user->role()->attach(5);

        return response()->json([
            'success' => true,
            'errors' => null
        ]);
    }
}

UPDATE:

I have fixed my route issue however I'm trying to figure out how to keep it from moving onto the next page in the form wizard if there is a validation error with one of the form fields that is being passed. Because when the user comes to the first page of the form and don't fill out any of the fields it should return with the errors. So I'm trying to figure out with the validation how to pass it back with the errors to the ajax request so that it can display the corresponding errors next to the input field.

  • 写回答

2条回答 默认 最新

  • douji8549 2016-11-15 15:49
    关注

    405 Method Not Allowed exception indicates that a route doesn't exist for the HTTP method you are requesting.

    Make sure your route is a post request.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?