I read this answer
And I did this:
function countryPage() {
$.ajax({
url: ".php?action=parse&disablelimitreport=true&format=json&prop=text|langlinks&noimages=true&mobileformat=true&page="+ curTitle + "&callback=?",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: countryPageSuccess
});
}
function countryPageSuccess(counterObject, data) {
$.each(data, function(i, item) {...
But if I then do as per that answer
$.each(JSON.parse(data), function(i, item) {
I get
Uncaught SyntaxError: Unexpected token o in JSON at position 1
I read this answer
And I did this:
function countryPage() {
$.ajax({
url: "https://en.wikipedia/w/api.php?action=parse&disablelimitreport=true&format=json&prop=text|langlinks&noimages=true&mobileformat=true&page="+ curTitle + "&callback=?",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: countryPageSuccess
});
}
function countryPageSuccess(counterObject, data) {
$.each(data, function(i, item) {...
But if I then do as per that answer
$.each(JSON.parse(data), function(i, item) {
I get
Share Improve this question edited Aug 3, 2017 at 2:39 rob.m asked Aug 3, 2017 at 2:30 rob.mrob.m 10.6k21 gold badges88 silver badges175 bronze badges 13Uncaught SyntaxError: Unexpected token o in JSON at position 1
-
Log
data
and take a look at what it contains, more than likely its already an object – Patrick Evans Commented Aug 3, 2017 at 2:34 -
@PatrickEvans ok but if I don't do
JSON.parse
I get the error in the question title and yes I confirm it is already an object – rob.m Commented Aug 3, 2017 at 2:35 -
The 2nd argument to the
success
callback is the textStatus. it won't be JSON. The JSON response should already be deserialized in thecounterObject
– Phil Commented Aug 3, 2017 at 2:36 - what do you mean is the textStatus? @Phil – rob.m Commented Aug 3, 2017 at 2:37
- 1 The data you are getting from the API is the first argument not the second (which you called data). Look at the jQuery ajax reference to see how the success callback is defined – Patrick Evans Commented Aug 3, 2017 at 2:39
1 Answer
Reset to default 2It's already a JSON object. You cannot parse it again.
to receive HTTP error code, provide a error callback function next to success
, showed my code below.
function countryPage() {
$.ajax({
url: "https://en.wikipedia/w/api.php?action=parse&disablelimitreport=true&format=json&prop=text|langlinks&noimages=true&mobileformat=true&page=1&callback=?",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: countryPageSuccess,
error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.status);
alert(textStatus);
alert(errorThrown);
}
});
}
function countryPageSuccess(data, result) {
$.each(data, function(i, item) {
console.log(item);
});
}
countryPage();
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745320570a4622425.html
评论列表(0条)