简体   繁体   English

JSON 中的意外标记 r 位于位置 0(...)

[英]Unexpected token r in JSON at position 0(…)

I'm now developing a web server based on STM32 MCU.我现在正在开发基于 STM32 MCU 的 Web 服务器。 The browser send request to MCU then MCU response a web html file.浏览器向 MCU 发送请求,然后 MCU 响应 web html 文件。 User can further set the parameters and using form to submit the parameters back to MCU for broadcasting.用户可以进一步设置参数并使用表单将参数提交回 MCU 进行广播。 Now I got a problem with it.现在我遇到了问题。 I try to send the data of form in JSON data type.我尝试以 JSON 数据类型发送表单数据。 But somehow it return me an error.但不知何故,它返回了一个错误。 "Unexpecteded token r in JSON at position 0(...)". “JSON 中的意外标记 r 在位置 0(...)”。 Here is my code for submit.这是我的提交代码。

$(document).ready(function(){
    // click on button submit

    $("#broadcastform").on('submit', function(e){
        e.preventDefault();
        $.ajax({
            url: '192.168.0.10',
            type : "POST",
            dataType : 'json',
            data: $(this).serialize(),
            success : function(result) {
            console.log(result);
            alert($(this).serialize());
            },
            error: function(xhr, resp, text) {
            console.log(xhr, resp, text);

        }
    })
});
   });

You can see I set the url as 192.168.0.10 which is the ip of my MCU Platform.您可以看到我将 url 设置为 192.168.0.10,这是我的 MCU 平台的 ip。 And here is my form code.这是我的表单代码。

<form name="broadcastform" id="broadcastform" method="post" action="">  


    <h1 id="broadcast_title" style="color:rgba(255,255,255,0.7);font-size: 250%;font-weight: 400;margin-top:-10px" align="middle">BROADCAST</h1>
    <hr style="border-color:#ffffff;weight:40%;margin:0 auto;margin-bottom:20px">
    <center class="page_intro">
    <div style="margin-top:-1%;color:rgba(255,255,255,0.7);width:90%;margin-bottom:12.5%" class="page_intro">
    <font size="6" style="line-height: 150%"class="page_intro"><center>Welcome!</center></font>
    <font size="5" style=" padding-top:20px;line-height: 150%;font-weight:normal;opacity:0.7"class="page_intro"><center>This is a Tool to Configure and Broadcast Your Modulator. Please Follow the Steps and Fill in the Parameter Fields for Your Preference. Enjoy the Tour !</center></font>
    </div>
    </center>
    <!-- Page Basic Setting --> 
    <select name="InputSource"  class="required page_basic" style="margin-left:23%" form="broadcastform" >

                <option value="">Broadcast Input</option>             
                <option value="0">HDMIPhy</option>             
                <option value="1">USB Streaming</option>             
                <option value="2">MPEC-TS Interface</option>             
                <option value="3">VIP(Ethernet)</option>         
    </select>
    <select name="ModulationMode"class= "page_basic required" style="margin-left:23%" form="broadcastform">             
                <option value="">Modulation Mode</option>             
                <option value="1">ATSC</option>             
                <option value="2">DTMB</option>             
                <option value="3">DVB</option>             
                <option value="4">ISDB</option>         
    </select>           
    <input type= "text" name= "ProviderName" placeholder="Provider Name" maxlength="16" class="required page_basic">      
    <input type= "text" name= "ServiceName" placeholder="Service Name" maxlength="16" class="required page_basic" style="margin-bottom:8%">

    <!-- Page IP Setting. Only with ETH Input Source--> 
    <input type= "text" name= "LocalIP" class="page_ip" placeholder="Local IP" style="margin-top:30px"  id="LocalIp">             
    <input type= "text" name= "RemoteVIPAddr" class="page_ip" style="margin-top:7%" placeholder="Remote VIP Address" id="RemoteIp">
    <input type= "text" name= "RemoteVIPPort" class="page_ip" style="margin-top:7%;margin-bottom:11.8%"  placeholder="Remote VIP Port"id="RemoteVIPPort">

    <!-- Page RF Setting -->              
    <input type= "text" name= "RFOutFreq" class="page_rf" style="margin-top:7%" placeholder="RF Output Frequency"  id="RFOutFreq">
    <input type= "text" name= "RFIfFreq" class="page_rf"style="margin-top:7%" placeholder="RF IF Frequency" id="RFIfFreq">         
    <input type= "text" name= "RFBandwidth" class="page_rf" style="margin-top:7%;margin-bottom:11.8%" placeholder="RF Bandwidth" id="RFBandwidth">   

    <!-- Page EncryptKey Setting -->              
    <input type= "text" name= "EncryptKeyLo" class="page_encrypt" style= "margin-top:13%" placeholder="Encrypt Key Low" id="EncryptKeyLo">  
    <input type= "text" name= "EncryptKeyHi" class="page_encrypt" style=" margin-top:13%;margin-bottom:16.1%" placeholder="Encrypt Key High" id="EncryptKeyHi">
<input id="submit" type="submit" value="Submit" class="btn inner" />
</form>

Anyone has idea about it?任何人都知道吗? been stuck at this point for a long time.卡在这点很久了。

Also, when I added console.log($(this).serialize());另外,当我添加 console.log($(this).serialize()); in my ajax code "error" part(which will be executed if i press submit).在我的 ajax 代码“错误”部分(如果我按提交将执行)。 It shows nothing.它什么也没显示。 It seems like a empty object.它看起来像一个空物体。 But I just do the form.submit();但我只是做 form.submit(); My MCU can receice "x-www-form-urlencoded" data.我的 MCU 可以接收“x-www-form-urlencoded”数据。

I checked the console again.我再次检查了控制台。 The response text is not in json form.响应文本不是 json 格式。 It is still in urlencoded form.它仍然是 urlencoded 形式。

This is most likely an issue with the response being a success, not an error, but the server didn't return a response body.这很可能是响应成功而不是错误的问题,但服务器没有返回响应正文。 Trying to parse an undefined result will cause these sorts of errors in JSON parsers.尝试解析undefined结果将导致 JSON 解析器中出现此类错误。

Check the network tab in your browser's inspector and see if the response is a 204 No Content .检查浏览器检查器中的网络选项卡,看看响应是否为204 No Content If it is, you'll need to remove the dataType : 'json' line from your AJAX call and handle the response manually in your success handler.如果是,您需要从 AJAX 调用中删除dataType : 'json'行,并在success处理程序中手动处理响应。 Only parse the response data if the the response code isn't 204 .如果响应代码不是204则仅解析响应数据。

success: function(data, textStatus, xhr) {
    if (xhr.status !== 204) {
        var obj = JSON.parse(data);
    }
},

I have no idea what version of JQuery you're using so this example may not work in all versions, but the principle is the same.我不知道您使用的是哪个版本的 JQuery,因此此示例可能不适用于所有版本,但原理是相同的。 Check for 204 status code, only parse the JSON data if there is a body.检查204状态码,只有在有 body 时才解析 JSON 数据。

Note: According to the JQuery documentation for $.ajax() :注意:根据$.ajax()的 JQuery 文档:

As of jQuery 1.9, an empty response is also rejected;从 jQuery 1.9 开始,空响应也会被拒绝; the server should return a response of null or {} instead.服务器应改为返回 null 或 {} 响应。

However, I don't know if "rejected" in this case means calling the error method.但是,我不知道在这种情况下“拒绝”是否意味着调用error方法。

If all else fails, simply remove the dataType: 'json', setting from your $.ajax() call to tell JQuery not to try and parse the response as JSON.如果所有其他方法都失败了,只需从$.ajax()调用中删除dataType: 'json', setting 以告诉 JQuery 不要尝试将响应解析为 JSON。

The Error错误

Unexpected token r in JSON at position 0(…) JSON 中的意外标记 r 位于位置 0(...)

It means your request get an unexpected response, which means this message is a kind of error response code.这意味着您的请求得到了意外的响应,这意味着此消息是一种错误响应代码。 To resolve this you need to catch the response code and response header/body on the network.要解决此问题,您需要在网络上捕获响应代码和响应标头/正文。

For example , once your request payload is overloading and the response give 413 error code but no response body.例如,一旦您的请求负载超载并且响应给出 413 错误代码但没有响应正文。 Trying to display response body would give you an unexpected token as the display.尝试显示响应正文会给您一个意外的标记作为显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM