我现在用的是如下因素的标准XHR请求错误解析JSON只能用异步XHR请求
//retrieve markerData from JSON
function retrieveMarkers(){
var markersXMLhttp = new XMLHttpRequest();
//open the request and store parameters for the request. GET (or POST) is method to send,
//the pathway or url of the data is specified, and whether or not the request is asynchronous
markersXMLhttp.open("GET", "../../map/json/myMapMarker.json", false);
//send the request
markersXMLhttp.send();
//there conditions allow the request to wait until the server respondes that it is ready.
if(markersXMLhttp.readyState == 4 && markersXMLhttp.status == 200){
//the response is stored in a variable
var XMLmarkersResult = markersXMLhttp.responseText;
}
//convert JSON string to javascript object
var markersResult = JSON.parse(XMLmarkersResult);
return markersResult;
}
我有异步设置为false,所以我得到了以下错误拉标记物体关闭数据库的
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user’s experience.
我同意Mozilla!所以让我们将Asynch改为真。 O快照,现在我得到这个错误。
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
这到底是怎么回事,我的JSON文件没有任何不同。异步处理json的方式不同吗?我喜欢解决这个问题,以免我的请求中出现任何错误。以下我会发布我的JSON代码样本,以防问题出在那里。
{
"myMarkers" : [
{
"index" : "000",
"position" : {
"lat" : 45.5,
"lng" : -122.61
},
"description" : "Portland OR",
"infoWindow" : "The origin of the journey, where my roots are, and were many great people live"
},
{
"index" : "001",
"position" : {
"lat" : 44.5,
"lng" : -121.61
},
"description" : "A New Place",
"infoWindow" : "The duplicat will carry for the replicons... until the safe find the fires of the fury"
}
]
}
感谢您对答案的解释。我试图理解代码背后的机制和推理,这无疑帮助我看到发生了什么。 – DMrFrost