ajax跨域post+get請求

 網上多解決方法是加上

xhrFields: {
    withCredentials: true
}

但是加的位置如果是錯誤的也沒用,所以記錄一下正確的跨域請求方式

                $.ajax({
                    xhrFields: {
                        withCredentials: true
                    },
                    data: JSON.stringify(
                        this.prodectData
                    ),
                    type: 'post',
                    dataType: 'json',
                    contentType: "application/json",
                    url: this.baseUrl + `/admin/addOrUpdateProduct`,
                    success: function (data) {
                        console.log(data)
                        if (confirm("操作成功,是否退出當前頁面?")) {
                            window.close();
                        }
                    }
                });

                $.ajax({
                    url: $then.baseUrl + `/admin/getProduct?id=` + urlid,
                    dataType: 'json',
                    xhrFields: {
                        withCredentials: true
                    },
                    processData: false,
                    type: 'get',
                    success: function (data) {
                        $then.prodectData = data
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status);
                        alert(XMLHttpRequest.readyState);
                        alert(textStatus);
                    }
                });

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章