简体   繁体   English

通过 ajax 发送数据时,JSON 输入意外结束

[英]Unexpected end of JSON input when sending data via ajax

<script>
$.ajax({
    type: "POST",
    url: "http://localhost:3000/xot/us/confirm",
    crossDomain: true,
    dataType : 'json',
    contentType: "application/json; charset=utf-8",
    data:JSON.stringify({
      username:"User"
    }),
    success: function(result, status, xhr) {
      console.log(result.address1);
    },
    error: function(xhr, status, error) {
      console.log(error);
    }
  })
</script>

If you are using nodejs as a backend you should add header there如果您使用 nodejs 作为后端,则应在此处添加 header

app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept'
);
next();  
});

And in Ajax Request.. Your request looks will look like this在 Ajax 请求中。您的请求看起来像这样

    $.ajax({
    type: "GET",
    url: "http://localhost:3002/category",
    crossDomain: true,
    contentType: "application/json; charset=utf-8",
    data:JSON.stringify({
    username:"User"
    }),
    success: function(result, status, xhr) {
    console.log(result);
    },
    error: function(xhr, status, error) {
    console.log(error);
    }
    })

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

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