Неожиданный конец ввода JSON при отправке данных через ajax - PullRequest
0 голосов
/ 04 ноября 2019
<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>

1 Ответ

0 голосов
/ 04 ноября 2019

Если вы используете nodejs в качестве бэкэнда, вы должны добавить туда заголовок

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();  
});

И в Ajax Request. Ваш запрос будет выглядеть так

    $.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);
    }
    })
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...