SyntaxError:位于0的JSON中的意外标记W.

问题描述 投票:0回答:1

我在JavaScript中发送带有API的POST请求,并且我已经使用以下方法发出了许多POST请求,但现在显示错误:SyntaxError:位于0的JSON中的意外令牌W

这是我的代码 -

function updateAlertSetting(data_json, callback){

var firebase_token = getCookie("firebase_token");
if(firebase_token===null){
    window.location.href="login.html";
    return;
}
var base_64_firebase_token = btoa(firebase_token);

console.log(typeof data_json);  // Object
console.log(data_json);         // prints the data_json

$.ajax(
    {
        type: 'POST',
        url: getApiURL(28),
        data: {
                "alertSetting_value": data_json.alertSetting_value,
                "alertSetting_name": data_json.alertSetting_name,
                "firebase_token": data_json.firebase_token
              },
        headers: { "Authorization": "Basic " + base_64_firebase_token },
        success : function(newData){
            callback(newData);
        },
        error: function (xhr,ajaxOptions,throwError){
            console.log(throwError);
            console.log("Error!!!");
        }
    }
);
}
javascript ajax http-post
1个回答
0
投票

我做了以下代码:

var xmlhttp;
    try{
        xmlhttp = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                //Browser doesn't support ajax  
                alert("Your browser is unsupported");
            }
        }
    }       
    if(xmlhttp){
        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState===4 && xmlhttp.status===200) {
                callback(xmlhttp.responseText);
            }
            if(xmlhttp.status === 404){
                alert("It couldn't connect to the server.");
            }               
        }
        xmlhttp.open("POST",getApiURL(28),true);
        xmlhttp.setRequestHeader("Authorization","Basic " + base_64_firebase_token);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("alertSetting_value="+data_json.alertSetting_value+"&alertSetting_name="+data_json.alertSetting_name+"&firebase_token="+data_json.firebase_token);
    }

现在这是有效的,谢谢@jro。

© www.soinside.com 2019 - 2024. All rights reserved.