ajax(jquery) post json 跨域访问问题

因为后端需要json或raw格式数据,按照网上的例子,发送的总是form-data数据

设置contentType:"application/json; charset=utf-8"后发现发送的不是POST请求,而是OPTIONS请求

最后还是看官网文档:http://api.jquery.com/jQuery.ajax/有一句ajax(jquery) post json 跨域访问问题_第1张图片

<html>
<head>
<script src="jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.ajax({
        type: "POST",
        url: "http://121.45.108.14:8000/weiapi/login",
        async: true,
        data: JSON.stringify({                  
            phone: "18625531123",
            password: "74c665982bb15059e63"
        }),
        contentType: "text/plain; charset=utf-8",
        dataType: "json",
        success: function(data) {
        }
    });
  });
});
</script>
</head>
<button>向页面发送 HTTP POST 请求,并获得返回的结果</button>
</html>


服务器端需要添加头 Access-Control-Allow-Origin:* 表示允许跨域访问

你可能感兴趣的:(ajax(jquery) post json 跨域访问问题)