1. 程式人生 > >加入跨域支援後ajax請求返回Uncaught SyntaxError: Unexpected token :

加入跨域支援後ajax請求返回Uncaught SyntaxError: Unexpected token :

本來正常的程式碼,加入跨域支援dataType: 'JSONP',

$.ajax({
//contenType: "application/x-www-form-urlencoded",
            type: "GET",
            dataType: 'JSONP',
            url: 'http://'+element.vm.ip+':8034/Monitor/StartMonitorMachine',
            //url: 'http://localhost:58949/Monitor/StartMonitorMachine',
            
            data: {
                "configId": stdetailsvm.monitorConfigList[i].id,
                "ReCallUrl": "http://172.16.13.10:8008",
                "ip":element.vm.ip
            },
            success: function (data) {
                if (data.Data == "0") {
  
                } else {
                    alert("向伺服器傳送監控指令失敗。"+data.Message);
                }
            },
            error: function (data) {
                alert(data.retMSG);
            }
        });

返回資料如下:{"Data":"0","StateCode":0,"Message":""}

但是Console裡面有Uncaught SyntaxError: Unexpected token :報錯

原因:Message空導致

解決方法:使Message返回資料