简体   繁体   English

SyntaxError:“JSON.parse:JSON 数据第 1 行第 1 列的意外字符”与 Fetch api

[英]SyntaxError: “JSON.parse: unexpected character at line 1 column 1 of the JSON data” with Fetch api

I'm trying to pass some HTML form inputs with Javascript using Fetch api to the server side (Flask).我正在尝试使用 Fetch api 将一些带有 Javascript 的 HTML 表单输入传递到服务器端(Flask)。 When I send a POST request, it works perfect and I can receive the data as well, but it also gives me this error statement on Mozilla Firefox:当我发送 POST 请求时,它工作正常,我也可以接收数据,但它也在 Mozilla Firefox 上给了我这个错误声明:

SyntaxError: "JSON.parse: unexpected character at line 1 column 1 of the JSON data" SyntaxError:“JSON.parse:JSON 数据的第 1 行第 1 列出现意外字符”

And on Google Chrome it gives me:在谷歌浏览器上它给了我:

SyntaxError: Unexpected token < in JSON at position 0语法错误:位置 0 处的 JSON 中出现意外标记 <

Here is my Fetch api code:这是我的 Fetch api 代码:

$(document).ready(function() {
    const myForm = document.getElementById("vform");
    myForm.addEventListener('submit', function(e){

    let phonenumber = document.getElementById('phoneNumber').value;
    let password = document.getElementById('password').value;
    let checked = document.getElementById('rememberMe').value;

    if(typeof phonenumber, password, checked !== 'undefined' && phonenumber, password, checked !== null) {

    var data = [{
                    phoneNo: phonenumber,
                    password: password,
                    checked: checked,
                }];

    fetch(`${window.origin}/login`, {
        method:'POST',
        headers: {
            "Content-Type": "application/json" 

        },

        body: JSON.stringify(data)
    })
    .then((res) => res.json())

    .then((data) => console.log(data))

    .catch((err)=>console.log(err))

    e.preventDefault();
    }
 });
});

And this is how I receive data on the server side:这就是我在服务器端接收数据的方式:

@app.route("/login", methods=["GET", "POST"])
def login():
  if request.get_json() != None:     
    req = request.json()
    phoneNo = req["phoneNo"]
    password = req["password"]
    checked =  req["checked"]


    print(phoneNo)
    return render_template("index.html", req=req)

Why is that happening?为什么会这样? Can you please help me?你能帮我么?

For Troubleshooting api I prefer u to use postman.In your case your rendering function passing a html file which is not in json format.In flask we use object serialization library to convert non json data to json.Marshmallow is an ORM/ODM/framework-agnostic library for converting complex datatypes, such as objects, to and from native Python datatypes.But in ur case it will not help.However for ur problem check this blog post hope it will help.对于故障排除 api,我更喜欢你使用邮递员。在你的情况下,你的渲染函数传递一个不是 json 格式的 html 文件。在烧瓶中,我们使用对象序列化库将非 json 数据转换为 json。Marshmallow 是一个 ORM/ODM/框架- 用于将复杂数据类型(例如对象)转换为本地 Python 数据类型或从本地 Python 数据类型转换的不可知库。但在您的情况下,它无济于事。但是对于您的问题,请查看此博客文章,希望它能有所帮助。 blog post 博文

暂无
暂无

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

相关问题 SyntaxError:JSON.parse:JSON数据第3行第1列的意外字符 - SyntaxError: JSON.parse: unexpected character at line 3 column 1 of the JSON data SyntaxError:JSON.parse:JSON数据的第1行第2列出现意外字符 - SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data SyntaxError:JSON.parse:JSON数据的第1行第1列出现意外字符吗? - SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data? 语法错误json.parse json数据第1行第1列的意外字符 - syntaxerror json.parse unexpected character at line 1 column 1 of the json data 语法错误:JSON.parse:JSON 数据的第 1 行第 1 列出现意外字符 - SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data 获取语​​法错误:JSON.parse:JSON 数据第 1 行第 1 列的意外字符 - Fetch SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data 使用 Fetch: SyntaxError: JSON.parse: JSON 数据第 1 行第 1 列的意外字符 - Using Fetch: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data SyntaxError: JSON.parse: JSON 第 1 行第 2 列的意外字符 - SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON JSON错误:SyntaxError:JSON.parse:JSON数据的第2行第1列出现意外字符 - JSON error : SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data SyntaxError: JSON.parse: JSON 数据的第 1 行第 1 列的数据意外结束,使用 fetch - SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data using fetch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM