如何将withCredentials = true添加到Jquery .load()?

问题描述 投票:2回答:1

我对Jquery很新,

我使用Jquery .load()从另一个域获取一些内容,我需要使用会话cookie。任何人都知道如何将“withCredentials = true”添加到.load()函数中?

EG

$('#displayPics').load('https://example.com/somecontent.php').fadeIn("slow");

谢谢

jquery ajax xmlhttprequest
1个回答
2
投票

$.load()是一个非常简化的速记功能,它不允许你通过设置。

只需使用$.ajax()重新创建它的功能:

$.ajax({
  url: 'https://example.com/somecontent.php',
  xhrFields: {
     withCredentials: true
  },
  success: function (data, status) {
    $('#displayPics').html(data.response).fadeIn();
  } 
});

考虑到这一点,有可能你可以使用$.ajaxSetup(),看看它是否适用于负载(没有亲自尝试过它们)。在调用加载函数之前将其放入:

$.ajaxSetup({
  xhrFields: {
     withCredentials: true
  }
});

请记住,这将影响来自jQuery的所有AJAX查询,这可能是您想要的,也可能不是。

© www.soinside.com 2019 - 2024. All rights reserved.