AJAX的跨域问题使用设置响应头和JsonP的方式实现跨域后端数据源编写------AJAX

134 篇文章 0 订阅
45 篇文章 0 订阅
文章介绍了如何在JavaServlet中使用JSONP处理跨域请求,通过`HttpServletRequest`和`HttpServletResponse`实现数据传递,并设置CORS头允许特定域名访问。
摘要由CSDN通过智能技术生成

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
 
import java.io.IOException;
import java.io.PrintWriter;
 
@WebServlet("/jsonP1")
public class jsonP1 extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
//        jsonP跨域的时候只支持get请求,因为我们写src本质就是一个get请求的传递
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        //如果是输出前端代码呢?
//        out.println("alert(123);");
//        out.println("sayHello();");
        String fun = request.getParameter("fun");
//        out.println(fun + "({\"name\" : \"Jack\"});");
        out.println(fun + "(5,6);");
    }
}

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
 
import java.io.IOException;
import java.io.PrintWriter;
 
@WebServlet("/jsonP1")
public class jsonP1 extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
//        jsonP跨域的时候只支持get请求,因为我们写src本质就是一个get请求的传递
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        //如果是输出前端代码呢?
//        out.println("alert(123);");
//        out.println("sayHello();");
        String fun = request.getParameter("fun");
//        out.println(fun + "({\"name\" : \"Jack\"});");
        out.println(fun + "(5,6);");
    }
}

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
 
import java.io.IOException;
 
@WebServlet("/hello")
public class Hello extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应头
        response.setHeader("Access-Control-Allow-Origin","http://localhost:8080");
        //跨域访问的资源允许访问,设置响应头的方式原理是
        response.getWriter().println("Halla Ajax");
    }
}

 import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
 
import java.io.IOException;
 
@WebServlet("/hello")
public class Hello extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应头
        response.setHeader("Access-Control-Allow-Origin","http://localhost:8080");
        //跨域访问的资源允许访问,设置响应头的方式原理是
        response.getWriter().println("Halla Ajax");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值