1. 程式人生 > >Ajax請求跨域問題,報錯XMLHttpRequest cannot load ''. No 'Access-Control-Allow-Origin' header is present on t

Ajax請求跨域問題,報錯XMLHttpRequest cannot load ''. No 'Access-Control-Allow-Origin' header is present on t

報錯:XMLHttpRequest cannot load  ''. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' ' is therefore not allowed access. 

原因:ajax預設不支援跨域訪問,相應解決這問題很簡單,加上dataType: 'JSONP'就可以完美解決

$.ajax({
            type: "GET",
            dataType: 'JSONP',
            url: 'http://'+element.vm.ip+':8034/Monitor/StartMonitorMachine',
             
            data: {
                "configId": stdetailsvm.monitorConfigList[i].id,
                "itemId": itemId,
                 "startTime": stdetailsvm.currentTime(),

                "ip":element.vm.ip
            },
            success: function (data) {
                if (data.Data == "0") {
  
                } else {
                    alert("向伺服器傳送監控指令失敗。"+data.Message);
                }
            },
            error: function (data) {
                alert(data.retMSG);
            }
        });