Как отобразить ответную страницу сервером, используя запрос GET в вызове ajax - PullRequest
0 голосов
/ 22 сентября 2018
function regCall(token){
    $.ajax({
        type: 'GET',
        url: 'http://localhost:3000',
        dataType: 'HTML',
        headers: {
            'x-auth': token
        }
    });
}

Это мой ajax-запрос GET, я хочу отобразить данный URL в html.Ниже приведен весь фрагмент с моей логикой входа в систему.

$(document).ready(()=>{
$('#login').submit((e)=>{
$.ajax({
    type: 'POST',
    url:'http://localhost:3000/login/users',
    data: {
      email: $('#email').val(),
      password: $('#password').val()
    },
    success: function(data, status, req){
      // alert(req.getResponseHeader('x-auth'));
      localStorage.setItem('t',req.getResponseHeader('x-auth'));
      var token = localStorage.getItem('t');
      regCall(token);
      // window.location.href = '/';

    },
    error: function (req, status, error) {
      // alert(req.getResponseHeader('x-auth'));
      localStorage.setItem('t',req.getResponseHeader('x-auth'));
      alert('Invalid email and password');
      window.location.href = '/login';
    }
   });
  e.preventDefault();
 });
})

Это весь код фрагмента.

Ответы [ 2 ]

0 голосов
/ 22 сентября 2018
By using the success callback function you can display the response content on the HTML place

**First method:**

function regCall(token){
    $.ajax({
        type: 'GET',
        url: 'http://localhost:3000',
        dataType: 'HTML',
        headers: {
            'x-auth': token
        },
        success: function(responseData){
           $("#div or class Id").html(responseData);
        }

    });
}


**Second method:**

function regCall(token){
    $.ajax({
        type: 'GET',
        url: 'http://localhost:3000',
        dataType: 'HTML',
        headers: {
            'x-auth': token
        }
    }).done(function(responseData){
     $("#div or class Id").html(responseData);
   });
}

**NOTE:**
Make sure you are having the jQuery script
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
0 голосов
/ 22 сентября 2018

извлечение данных ответа из функции SUCCESS:

function regCall(token){
    $.ajax({
        type: 'GET',
        url: 'http://localhost:3000',
        dataType: 'HTML',
        headers: {
            'x-auth': token
        },
        success: function(data){
            //targetElement should be replaced by the ID of target element
            $("#targetElement").html(data);
        }
    });
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...