Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at some URI

两盒软妹~` 提交于 2019-12-13 23:35:18

问题


I wrote this following ajax request:

$.ajax({    
    type : "POST",
    processData:false,        
    crossDomain:true,
    crossOrigin:true,
    contentType:false,          
    headers: { 
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "POST",
        "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",   
    },
    url : 'my URI',
    data: formData,
    success : function(receivedData) {
        console.log("SUCCESS: ");
        alert(receivedData);
    }
});

But in the reply I'm getting this following message in by browser : Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at my URI. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).


回答1:


Try below code if it will not work then you need to implement server side cors enabling

$.ajax({    
        type : "POST",
        processData:false,

        crossDomain:true,
        crossOrigin:true,
        contentType:false,
        'Access-Control-Allow-Methods': 'POST',
        'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',   
        },
        header:{'Access-Control-Allow-Origin': '*'},
        url : 'my URI',
        data: formData,
        success : function(receivedData) {
            console.log("SUCCESS: ");
            alert(receivedData);

            }

});

then you can use ajax call like that

$.ajax({
        url: 'http:ww.abc.com?callback=?',
        dataType: 'JSONP',
        jsonpCallback: 'callbackFnc',
        type: 'GET',
        async: false,
        crossDomain: true,
        success: function () { },
        failure: function () { },
        complete: function (data) {
            if (data.readyState == '4' && data.status == '200') {
                errorLog.push({ IP: Host, Status: 'SUCCESS' })
            }
            else {
                errorLog.push({ IP: Host, Status: 'FAIL' })
            }
        }
});


来源:https://stackoverflow.com/questions/40894765/cross-origin-request-blocked-the-same-origin-policy-disallows-reading-the-remot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!