博客
关于我
ajax(jquery) post json 跨域访问问题
阅读量:795 次
发布时间:2019-03-25

本文共 840 字,大约阅读时间需要 2 分钟。

HTTP POST 请求问题解决方案

在开发过程中,当后端要求接收JSON或raw格式数据时,发送的数据类型通常是form-data。由于团队经验不足,可能会遇到以下问题:

  • Form-data数据格式的应用

    确保Form-data数据格式的使用是正确的,避免直接发送JSON数据。

  • 验证HTTP请求类型

    在设置contentType:"application/json; charset=utf-8"时,发现发送的不是POST请求,而是OPTIONS请求。这是因为某些浏览器或服务器会阻止跨域的非安全请求。

  • 检查服务器文档

    根据官方文档中的CORS配置,确保服务器端设置了正确的访问控制策略。部分服务器需要额外配置头部信息。

  • 发送HTTP POST请求的代码示例如下:

    $(document).ready(function() {
    $("button").click(function() {
    $.ajax({
    type: "POST",
    url: "http://121.45.108.14:8000/weiapi/login",
    async: true,
    data: {
    phone: "18625531123",
    password: "74c665982bb15059e63"
    },
    contentType: "text/plain; charset=utf-8",
    dataType: "json",
    success: function(data) {}
    });
    });
    });

    注意事项:

    • 服务器端需要添加头部信息Access-Control-Allow-Origin:*,表示允许跨域访问。
    • 建议测试提交请求时,工具栏开启“无网络”模式,避免浏览器缓存干扰。

    通过以上方法,可以有效解决HTTP POST 请求问题。

    转载地址:http://bmnyk.baihongyu.com/

    你可能感兴趣的文章
    Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
    查看>>
    NMAP网络扫描工具的安装与使用
    查看>>
    NMF(非负矩阵分解)
    查看>>
    nmon_x86_64_centos7工具如何使用
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.7 Parameters vs Hyperparameters
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>