NutzCN Logo
问答 我当前按照帮助文档做的这个微信登陆报错,Response for preflight is invalid
发布于 2045天前 作者 qq_6d75074b 2544 次浏览 复制 上一个帖子 下一个帖子
标签:

medicalrecord.html:1 XMLHttpRequest cannot load https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6a7b19b0fbd21040&redirect_uri=https%3A%2F%2F24736g26e1.zicp.vip%2Falfx%2Fwx%2Fweixin%2Faccess_token&response_type=code&scope=snsapi_base&state=2019#wechat_redirect. Response for preflight is invalid (redirect)

下面为代码,大神帮看看

package cn.wx;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.View;
import org.nutz.mvc.adaptor.VoidAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.weixin.impl.WxApi2Impl;
import org.nutz.weixin.impl.WxLoginImpl;
import org.nutz.weixin.spi.WxApi2;
import org.nutz.weixin.spi.WxLogin;
import org.nutz.weixin.spi.WxResp;
import org.nutz.weixin.util.Wxs;

import cn.base.bean.Patient;

@At("/wx")
@IocBean(create = "init")
public class WeixinModule { // 并不要求你继承任何东西
	/*
	 * wxHandler是被动请求的主要处理类, 里面写的1234567890就是"接口配置信息"里面提到的"token",
	 */
	@Inject
	protected DefaultWxHandler wxHandler;

	@Inject
	protected Dao dao;

	@Inject
	protected WxApi2 wxApi2;

	@Inject
	protected PropertiesProxy conf;

	public void init() {
		WxApi2Impl wxApi2 = new WxApi2Impl();
		wxApi2.configure(conf, "weixin."); // 从配置信息里面读取各种需要的参数
		this.wxApi2 = wxApi2;
	}

	// protected WxHandler wxHandler = new BasicWxHandler("1234567890");

	@At // 拼起来的全路径就是 /weixin/msgin
	@Filters
	@AdaptBy(type = VoidAdaptor.class)
	public View msgin(HttpServletRequest req) throws IOException {
		return Wxs.handle(wxHandler, req, "default");
	}

	/*
	 * 
	 * @At
	 * 
	 * @Filters public String getAccessToken() { return wxApi2.getAccessToken(); }
	 * 
	 * @At
	 * 
	 * @Filters public String getQrcodeUrl(@Param("doctorid") String doctorid) {
	 * WxResp wr = wxApi2.qrcode_create(doctorid, 0); return
	 * wxApi2.qrcode_show((String) wr.get("ticket")); }
	 * 
	 * @At
	 * 
	 * @Filters public NutMap getJsSDKConfig() { List<WxMenu> l = new ArrayList();
	 * WxMenu m = new WxMenu(); m.setName("首页"); m.setType("view");
	 * m.setUrl("https://24736g26e1.zicp.vip/alfx/wx/index.html"); l.add(m);
	 * wxApi2.menu_create(l); return
	 * wxApi2.genJsSDKConfig("https://24736g26e1.zicp.vip/alfx/wx/index.html",
	 * "scanQRCode", "getLocation"); }
	 */
	@At("/authorize")
	@Ok(">>:${obj}")
	@Filters
	public String authorize(HttpServletRequest req) {
		String redirect_uri = req.getRequestURL().toString().replace("authorize", "weixin/access_token");
		redirect_uri = redirect_uri.replace("http", "https");
		return wxLogin("weixin").authorize(redirect_uri, "snsapi_base", "2019"); // snsapi_base静默登录,snsapi_userinfo需要确认,后者才能获取用户详细信息
	}

	@At("/?/access_token")
	// @Ok(">>:/wx/html/medicalrecord.html?msg=${obj}")
	@Filters
	public String access_token(String prefix, @Param("code") String code, @Param("state") String state,
			HttpSession session) {
		WxResp resp = wxLogin(prefix).access_token(code);
		if (resp == null) {
			return "登录失败";
		}
		String openid = resp.getString("openid");

		Patient user = dao.fetch(Patient.class, Cnd.where("openid", "=", openid));
		if (user == null) {
			// 新用户 xxooo
		}

		// 执行登录操作 (普通方式)
		session.setAttribute("me", user);

		// 最后呢, 如果不是公众号的静默登录snsapi_base,还可以获取用户详细信息

		String access_token = resp.getString("access_token");
		resp = wxLogin(prefix).userinfo(openid, access_token);

		// 然后做你想做的事吧...
		return "ok";
	}

	protected WxLogin wxLogin(String prefix) {
		WxLoginImpl w = new WxLoginImpl();
		return w.configure(conf, prefix + ".");
	}

}
9 回复

这是哪里报的错误

authorize执行完没有根据路径调用 access_token 后台没有报错,return wxLogin("weixin").authorize(redirect_uri, "snsapi_base", "2019");
通过微信调试工具 报这个 XMLHttpRequest cannot load https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6a7b19b0fbd21040&redirect_uri=https%3A%2F%2F24736g26e1.zicp.vip%2Falfx%2Fwx%2Fweixin%2Faccess_token&response_type=code&scope=snsapi_base&state=2019#wechat_redirect. Response for preflight is invalid (redirect)

我把路径拷贝出来在微信web测试端执行正常,不知道哪里出了问题!

@Ok(">>:${obj}") 应该是这个重定向执行报的错误

这样写试试

@Ok("void")


String url = wxLogin("weixin").authorize(redirect_uri, "snsapi_base", "2019")';
log.info(url);
resp.sendRedirect(url);

还是不行

这是打印出来的url

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6a7b19b0fbd21040&redirect_uri=https%3A%2F%2F24736g26e1.zicp.vip%2Falfx%2Fwx%2Fweixin%2Faccess_token&response_type=code&scope=snsapi_base&state=vii1s95ki4j3pq14ccbf4spgf0#wechat_redirect

下面是微信调试工具显示的信息

jquery.min.js:6 XHR finished loading: GET "https://24736g26e1.zicp.vip/alfx/wx/authorize".
send @ jquery.min.js:6
ajax @ jquery.min.js:6
x.(anonymous function) @ jquery.min.js:6
call @ ajaxcall.js:16
(anonymous) @ medicalrecord.js:16
c @ jquery.min.js:4
fireWith @ jquery.min.js:4
ready @ jquery.min.js:4
q @ jquery.min.js:4
medicalrecord.html:1 XMLHttpRequest cannot load https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6a7b19b0fbd21040&redirect_uri=https%3A%2F%2F24736g26e1.zicp.vip%2Falfx%2Fwx%2Fweixin%2Faccess_token&response_type=code&scope=snsapi_base&state=vii1s95ki4j3pq14ccbf4spgf0#wechat_redirect. Response for preflight is invalid (redirect)
XHR failed loading: OPTIONS "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6a7b19b0fbd21040&redirect_uri=https%3A%2F%2F24736g26e1.zicp.vip%2Falfx%2Fwx%2Fweixin%2Faccess_token&response_type=code&scope=snsapi_base&state=vii1s95ki4j3pq14ccbf4spgf0".

不能ajax啊,微信登录要跳转的,ajax不行

话说,你这是做微信公众号登录呢?还是微信网页登录?

当前你的代码是网页登录

微信公众号登录 哈哈,应该是ajax的原因,改一下

公众号走的是jssdk哦

我给的例子是网页登录,误导你了

添加回复
请先登陆
回到顶部