NutzCN Logo
问答 nutz 接口POST 方式跨域访问提示错误?
发布于 2428天前 作者 qq_e3b6dba3 3024 次浏览 复制 上一个帖子 下一个帖子
标签:

curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "{
"idtbl_address": 75,
"user_id": 10,
"name": "111",
"telphone": "44444",
"address": "北固山路6",
"status": 0
}" "http://test.xboxes.cn/weixin/courier/address/75"

curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "{
  \"idtbl_address\": 75,
  \"user_id\": 10,
  \"name\": \"111\",
  \"telphone\": \"44444\",
  \"address\": \"北固山路6\",
  \"status\": 0
}" "http://test.xboxes.cn/weixin/courier/address/75"
33 回复

跨域是浏览器沙箱的事,curl没这个限制

这个是要填整那块?@wendal

我不知道你想查什么东西,哪里看出是跨域问题?

在自己搭建的接口平台中,做测试报错的提示

我把图片粘贴到群里了

浏览器访问的时候报错??

入口方法贴来看看

// 获取用户地址修改
@At("/address/?")
@POST
@Ok("json")

public Object updateAddress(int addressId,
       @Param("..") Tbl_weixin_user_address address,
       HttpServletRequest req, HttpServletResponse rsp) {
    // JSONObject j=new JSONObject();
    Json j = new Json();

    rsp.setHeader("Access-Control-Allow-Origin", "*");
    rsp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
    rsp.setHeader("Access-Control-Max-Age", "3600");
    rsp.setHeader("Content-Type:"," application/json");
    rsp.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization");
    rsp.setHeader("Access-Control-Allow-Credentials","true");
    int addressid = courierService.updateaddress(addressId, address);

    // JSONObject json = (JSONObject) JSONObject.toJSON(order);
    if (addressid > 0) {
       j.setInfo("地址修改成功!");
       j.setStatus("0");

    } else {
       j.setInfo("地址修改失败!");
       j.setStatus("1");

    }
    return j;
}

这是调用的接口@wendal

改成这样试试

@Filters(@By(type=CrossOriginFilter.class))
@At(value="/address/?", methods="OPTIONS")
@GET
public Object updateAddress() {}

// 获取用户地址修改
@Filters(@By(type=CrossOriginFilter.class))
 @At("/address/?")
 @POST
 @Ok("json")
public Object updateAddress(int addressId,
       @Param("..") Tbl_weixin_user_address address,
       HttpServletRequest req, HttpServletResponse rsp) {

你是要全部api都跨域吗??

我项目里没有CrossOriginFilter.class

哦, 那你加个动作链的处理器, 或者web.xml配个全局的filter比较

完了,大哥,你说这些,我还不太理解

web.xml配个全局的filter, 这种应该懂吧?

serlvet api里面的filter

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
weixin

nutz
org.nutz.mvc.NutFilter

modules
com.ruyima.MainModule



nutz
/*


WeChatFilter
com.gson.WeChatFilter


WeChatFilter
/wechat/*



*.html
UTF-8
false


*.jsp
UTF-8
false



404
/


500
/


index.jsp

发代码用 "插入代码"按钮啊!!!

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>weixin</display-name>
  <filter>
    <filter-name>nutz</filter-name>
    <filter-class>org.nutz.mvc.NutFilter</filter-class>
    <init-param>
      <param-name>modules</param-name>
      <param-value>com.ruyima.MainModule</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>nutz</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>WeChatFilter</filter-name>
    <filter-class>com.gson.WeChatFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>WeChatFilter</filter-name>
    <url-pattern>/wechat/*</url-pattern>
  </filter-mapping>
  <jsp-config>
    <jsp-property-group>
      <url-pattern>*.html</url-pattern>
      <page-encoding>UTF-8</page-encoding>
      <scripting-invalid>false</scripting-invalid>
    </jsp-property-group>
    <jsp-property-group>
      <url-pattern>*.jsp</url-pattern>
      <page-encoding>UTF-8</page-encoding>
      <scripting-invalid>false</scripting-invalid>
    </jsp-property-group>
  </jsp-config>
  <error-page>
    <error-code>404</error-code>
    <location>/</location>
  </error-page>
  <error-page>
    <error-code>500</error-code>
    <location>/</location>
  </error-page>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

这是现在项目的web.xml

加一个类, 然后把它配到web.xml, 放在其他filter之前

package org.nutz.mvc.filter;

import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionFilter;
import org.nutz.mvc.View;
import org.nutz.mvc.view.VoidView;

import java.io.FileFilter;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 如果是OPTIONS请求,那么返回自定义的Access-Control-Allow-*头部
 */
public class WebCrossOriginFilter implements Filter {

    private static final Log log = Logs.get();

    protected String origin;
    protected String methods;
    protected String headers;
    protected String credentials;

    public WebCrossOriginFilter() {
        this("*", "GET, POST, PUT, DELETE, OPTIONS, PATCH", "Origin, Content-Type, Accept, X-Requested-With", "true");
    }

    public WebCrossOriginFilter(String origin, String methods, String headers, String credentials) {
        this.origin = origin;
        this.methods = methods;
        this.headers = headers;
        this.credentials = credentials;
    }

    public View match(ActionContext ac) {
        HttpServletResponse resp = ac.getResponse();
        if (!Strings.isBlank(origin))
            resp.setHeader("Access-Control-Allow-Origin", origin);
        if (!Strings.isBlank(methods))
            resp.setHeader("Access-Control-Allow-Methods", methods);
        if (!Strings.isBlank(headers))
            resp.setHeader("Access-Control-Allow-Headers", headers);
        if (!Strings.isBlank(credentials))
            resp.setHeader("Access-Control-Allow-Credentials", credentials);
        
        if ("OPTIONS".equals(ac.getRequest().getMethod())) {
            if (log.isDebugEnabled())
                log.debugf("Feedback -- [%s] [%s] [%s] [%s]", origin, methods, headers, credentials);
            return new VoidView();
        }
        return null;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse resp = (HttpServletResponse)response;
        if (!Strings.isBlank(origin))
            resp.setHeader("Access-Control-Allow-Origin", origin);
        if (!Strings.isBlank(methods))
            resp.setHeader("Access-Control-Allow-Methods", methods);
        if (!Strings.isBlank(headers))
            resp.setHeader("Access-Control-Allow-Headers", headers);
        if (!Strings.isBlank(credentials))
            resp.setHeader("Access-Control-Allow-Credentials", credentials);
        
        if ("OPTIONS".equals(((HttpServletRequest)request).getMethod())) {
            if (log.isDebugEnabled())
                log.debugf("Feedback -- [%s] [%s] [%s] [%s]", origin, methods, headers, credentials);
            return;
        }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}

match方法忘记删了

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>weixin</display-name>
  <filter>
        <filter-name>WebCrossOriginFilter</filter-name>
        <filter-class>com.ruyima.weixin.controller.WebCrossOriginFilter</filter-class>
    </filter>
  <filter>
    <filter-name>nutz</filter-name>
    <filter-class>org.nutz.mvc.NutFilter</filter-class>
    <init-param>
      <param-name>modules</param-name>
      <param-value>com.ruyima.MainModule</param-value>
    </init-param>
  </filter>

filter-map也要写嘛

  <filter-mapping>
    <filter-name>WebCrossOriginFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

谢谢,大神!!!可以了

我靠,楼上的,你搞定了,不是应该,把完整的web.xml文件贴出来么?或者写个总结贴

那我来根据帖子总结下呗.

web.xml完整配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>weixin</display-name>
  <filter>
    <filter-name>WeChatFilter</filter-name>
    <filter-class>com.gson.WeChatFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>WeChatFilter</filter-name>
    <url-pattern>/wechat/*</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>nutz</filter-name>
    <filter-class>org.nutz.mvc.NutFilter</filter-class>
    <init-param>
      <param-name>modules</param-name>
      <param-value>com.ruyima.MainModule</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>nutz</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <jsp-config>
    <jsp-property-group>
      <url-pattern>*.html</url-pattern>
      <page-encoding>UTF-8</page-encoding>
      <scripting-invalid>false</scripting-invalid>
    </jsp-property-group>
    <jsp-property-group>
      <url-pattern>*.jsp</url-pattern>
      <page-encoding>UTF-8</page-encoding>
      <scripting-invalid>false</scripting-invalid>
    </jsp-property-group>
  </jsp-config>
  <error-page>
    <error-code>404</error-code>
    <location>/</location>
  </error-page>
  <error-page>
    <error-code>500</error-code>
    <location>/</location>
  </error-page>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

跨域工具类

package org.nutz.mvc.filter;

import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionFilter;
import org.nutz.mvc.View;
import org.nutz.mvc.view.VoidView;

import java.io.FileFilter;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 如果是OPTIONS请求,那么返回自定义的Access-Control-Allow-*头部
 */
public class WebCrossOriginFilter implements Filter {

    private static final Log log = Logs.get();

    protected String origin;
    protected String methods;
    protected String headers;
    protected String credentials;

    public WebCrossOriginFilter() {
        this("*", "GET, POST, PUT, DELETE, OPTIONS, PATCH", "Origin, Content-Type, Accept, X-Requested-With", "true");
    }

    public WebCrossOriginFilter(String origin, String methods, String headers, String credentials) {
        this.origin = origin;
        this.methods = methods;
        this.headers = headers;
        this.credentials = credentials;
    }
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse resp = (HttpServletResponse)response;
        if (!Strings.isBlank(origin))
            resp.setHeader("Access-Control-Allow-Origin", origin);
        if (!Strings.isBlank(methods))
            resp.setHeader("Access-Control-Allow-Methods", methods);
        if (!Strings.isBlank(headers))
            resp.setHeader("Access-Control-Allow-Headers", headers);
        if (!Strings.isBlank(credentials))
            resp.setHeader("Access-Control-Allow-Credentials", credentials);
        
        if ("OPTIONS".equals(((HttpServletRequest)request).getMethod())) {
            if (log.isDebugEnabled())
                log.debugf("Feedback -- [%s] [%s] [%s] [%s]", origin, methods, headers, credentials);
            return;
        }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}
添加回复
请先登陆
回到顶部