AJAX错误:parsererror:SyntaxError:意外的令牌<在位置0的JSON中

时间:2017-06-05 12:29:52

标签: javascript php jquery json ajax

我在使用模态提交表单时使用ajax函数它没有正确地转到url并且我在laravel中使用后端url

这是我的HTML代码

  <!-- Modal -->
    <div class="modal fade address" id="from_address" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Enter From Address</h4>
          </div>
          <div class="modal-body">
            <form method="post" action='{{ route("fromaddress.store") }}' name="from_address" id="from_address" novalidate="novalidate">
              <input type="hidden" id="user_id" name="user_id" value="{{Auth::user()->id}}"/>
              <input type="hidden" value="{{route('user.products.sell')}}" name="redirect_url"/>
              {{-- <input type="hidden" id="promocode" name="promocode" value="{{Cookie::get('promocode')}}"/> --}}
              <div class="form-group">
                <label for="from-name" class="control-label">Name:</label>
                <input type="text" class="form-control" name="from_name" id="from_name"/>
              </div>
              <div class="form-group">
                <label for="from-mobile" class="control-label">Phone:</label>
                <input type="text" class="form-control" name="from_phone" id="from_phone"/>
              </div>
              <div class="form-group">
                <label for="from-email" class="control-label">Email:</label>
                <input type="text" class="form-control" name="from_email" id="from_email"/>
              </div>
              <div class="form-group">
                <label for="from-state" class="control-label">State:</label>
                <input type="text" class="form-control" name="from_state" id="from_state">
              </div>
              <div class="form-group">
                <label for="from-country" class="control-label">Country:</label>
                <input type="text" class="form-control" name="from_country" id="from_country">
              </div>
              <div class="form-group">
                <label for="from-pincode" class="control-label">Zip:</label>
                <input type="text" class="form-control" name="from_zip" id="from_zip">
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <input type="submit" class="btn btn-primary"/>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>

这是我的jquery ajax代码

$("#from_address").on('submit', function(e){
        e.preventDefault(e);
        var url = $(this).attr('action');
        alert(url);
        var method = $(this).attr('method');
        var redirect_url = $(this).find("[name='redirect_url']").val();
        var myData = {
            user_id:$(this).find("[name='user_id']").val(),  
            from_name:$(this).find("[name='from_name']").val(),  
            from_email:$(this).find("[name='from_email']").val(),  
            from_state:$(this).find("[name='from_state']").val(),   
            from_country:$(this).find("[name='from_country']").val(),
            from_zip:$(this).find("[name='from_zip']").val(),
        }
        //console.log('data',myData);
        $.ajax({
            type: method,
            url: url,
            data: myData,
            success: function(data) {
              console.log("data",data);
               // alert("From address added successfully");
                //if(confirm(data.message)) {
                //    window.location.href = redirect_url;
                //}
            },
            error: function(jqXHR, textStatus, errorThrown) {
              console.log(JSON.stringify(jqXHR));
              console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
            }
        });
    });

后端代码是

public function store(Request $request)
    {

    $fromaddr = FromAddress::create([
            'from_name' => $request->from_name,
            'from_phone' => $request->from_phone,
            'from_email' => $request->from_email,
            'from_state' => $request->from_state,
            'from_country' => $request->from_country,
            'from_zip' => $request->from_zip,
            'user_id' => $request->user_id,
    ]);

      return Response::json([
        'message' => 'Shipping Address created',
      ], 200);
    }

在上面的代码中,它转到了成功功能,我的控制台消息没有显示数据我期望它显示完整的html代码作为控制台消息。任何人都可以帮我解决这个问题

0 个答案:

没有答案