【问题标题】:Getting Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.ajax.onload在 XMLHttpRequest.ajax.onload 的 JSON.parse (<anonymous>) 的位置 0 处的 JSON 中获取 Unexpected token <
【发布时间】:2021-02-02 03:35:47
【问题描述】:

我想使用 ajax 将其传递给控制器​​,但出现此错误。 responseText 的控制台日志并获取 html 代码。想弄清楚这是什么意思?

Javascript:

    // gathering the form data
                var ajaxData = new FormData( form );

                if( droppedFiles )
                {
                    Array.prototype.forEach.call( droppedFiles, function( file )
                    {
                        ajaxData.append( input.getAttribute( 'name' ), file );
                    });
                }
                
                console.log(ajaxData);
                // ajax request
                var ajax = new XMLHttpRequest();
                ajax.open( form.getAttribute( 'method' ), form.getAttribute( 'action' ), true );
                ajax.setRequestHeader("X-CSRF-TOKEN", $('meta[name="csrf-token"]').attr('content'));
                ajax.onload = function()
                {
                    form.classList.remove( 'is-uploading' );
                    if( ajax.status >= 200 && ajax.status < 400 )
                    {
                        var data = JSON.parse( ajax.responseText );
                        form.classList.add( data.success == true ? 'is-success' : 'is-error' );
                        if( !data.success ) errorMsg.textContent = data.error;
                    }
                    else alert( 'Error. Please, contact the webmaster!' );
                };

                ajax.onerror = function()
                {
                    form.classList.remove( 'is-uploading' );
                    alert( 'Error. Please, try again!' );
                };

                ajax.send( ajaxData );

索引

<form method="post" action="/contacts/{{$groupContactDetails->id}}/importContacts" enctype="multipart/form-data" novalidate class="box">
        <div class="box__input">
        <input type="file" name="files[]" id="file" class="box__file d-none" data-multiple-caption="{count} files selected" accept="application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
        <label for="file"><strong>Choose a file</strong><span class="box__dragndrop"> or drag it here</span>.</label>
        <button type="submit" class="box__button btn btn-info">Upload</button>
        </div>
        </form>

【问题讨论】:

  • 这是一个像“空指针”这样的一般错误——它经常发生,不仅在 Laravel 中,而且通常在 AJAX 中(来自 JSON.parse 的错误)——可能你已经从服务器获得了 HTML(通常以“ Network 并查看请求实际返回的内容。通常你会发现一个错误页面(在 HTML 中)。

标签: javascript ajax laravel ajaxform


【解决方案1】:

您收到来自服务器的格式错误或非 json 响应。您需要检查响应数据以确保您收到的是 JSON 格式的数据。

一种方法可以做到这一点:

    ajax.onload = function()
                {
                    form.classList.remove( 'is-uploading' );
                    if( ajax.status >= 200 && ajax.status < 400 )
                    {   
                         try {
                               var data = JSON.parse( ajax.responseText );
                              form.classList.add( data.success == true ? 'is-success' : 'is-error' );
                          } catch (e) {
                             console.log( ajax.responseText);
                            //data isn't json type, do error handling here
                         }
                        if( !data.success ) errorMsg.textContent = data.error;
                    }
                    else alert( 'Error. Please, contact the webmaster!' );
                };

【讨论】:

  • @LeviLooi 您的错误在于您尝试 JSON.parse 的数据,如果它无效,则捕获错误并将数据记录到控制台。记录的数据是什么?
  • 那是你的问题,只能通过JSON.parse解析JSON数据,html数据不需要JSON.parse,直接使用数据
猜你喜欢
  • 1970-01-01
  • 2022-08-12
  • 2021-07-20
  • 2021-04-06
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多