javascript - php js 跨域请求
ajax 请求本域名下的一个方法 userInfo,获取用户信息,但是在获取用户信息的userInfo方法,是使用了单点登录系统的,所以获取信息的过程存在跨域,这样ajax 请求就会报错,请问如何解决?
回复内容:
ajax 请求本域名下的一个方法 userInfo,获取用户信息,但是在获取用户信息的userInfo方法,是使用了单点登录系统的,所以获取信息的过程存在跨域,这样ajax 请求就会报错,请问如何解决?
我想到方法
1- 使用本地的php通过curl获取,这样ajax只要请求本地的php就不会发生跨域了,而PHP端的curl是没有跨域限制的。
2- 宿主端提供js脚本,伴生系统引用该js,并调用其中声明的方法进行ajax请求就不属于跨域了。
通常我都是用方法1,方法2需要宿主端系统的开发人员配合,太麻烦。
在php层做一个代理,让php请求那个跨域接口,然后php再把数据返回到页面上。
这个接口不能用jsonp,不是不能实现,是不安全。不能泄漏用户信息,因为是userinfo,所以必须注重安全问题。
跨域时,地址参数中要加一个回调函数,在js里,在这个回调函数里处理返回的数据。这里有一个post的跨域,可以参考一下。https://github.com/limeng0403/libs/blob/master/EOF/getJSON%E5%9C%A8PHP%E7%8E%AF%E5%A2%83%E4%B8%8B%E5%AE%9E%E7%8E%B0%E8%B7%A8%E5%9F%9F%E6%95%B0%E6%8D%AE%E5%8A%A0%E8%BD%BD.md
使用ajax jsonp
纯
GET
请求可以用jsonp
支持
CORS
的浏览器,可用CORS
请求数据步骤
发送请求--->服务器响应返回--->浏览器接受响应--->判断是否是同域下
是的话,就把响应数据返回给ajax。不是的话就提醒禁止跨域请求。
其实服务端可以在响应头中增加
header("Access-Control-Allow-Origin: 允许的域");
header("Access-Control-Allow-Methods: 允许请求的方法");
header("Access-Control-Allow-Headers: X-PINGOTHER");
告诉浏览器可以把响应值返回给此请求
参考:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS
跨域可以参考下面这篇文章
http://www.cnblogs.com/duanhuajian/p/4211616.html
header('content-type:application:json;charset=utf8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
在你的php文件中添加上这个
可以使用JSONP请求
我刚刚整理的解决跨域请求的三种方法:https://segmentfault.com/a/1190000004539815
postMessage();onmessage。
最好的是使用php curl代理过去,控制度高一点。
第二就是修改服务器接口文件,打开跨域限制,这样ajax就可以调用了。
第三就是建议楼主用jsonp了。
具体单点登录系统内部怎么实现的没有去看,奇怪的一点是,如果我通过浏览器直接访问userInfo方法 是可以获取用户的信息,然后ajax 再请求userInfo 方法 就能正常返回用户信息了,很是奇怪,所以我用了一个很笨的方法,就是在页面底部用了一个iframe,iframe加载应该是一次http请求,就正常了!但直接访问为什么报跨域的错误的原因还是没有找到,感谢大家的回答! @incNick @大猛 @张亚涛 @ 等等……

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
