I have a json file (text2.json) and a text file (text2.txt) which has the exact same content as text2.json.
I found it quite interesting when requesting json file via $.ajax:
Requesting text2.json to web server:
$.ajax({
url: "./scripts/text2.json",
dataType:"json",
success: function(data){
alert('success');
},
error: function(){alert('error');}
});
This alerts error.
Requesting text2.txt to web server:
$.ajax({
url: "./scripts/text2.txt",
dataType:"json",
success: function(data){
alert('success');
},
error: function(){alert('error');}
});
This alerts success.
As of now, I only have 2-day javascript experience and I was just wondering what causes this trade-off...
EDIT
For text2.json + type:"json", I revised my alert() in error: as you guys suggested. Then the alert gives "[object object]" as my json file is "{"result":true, "count":2}".That just looks like an empty object or something.
Then I detected there's a "parseerror" when requesing text2.json as json dataType. I am not sure about this info. Looks like some initial settings is incorrect. Anyone wanna give me a hint?
Looks like my json file is so much like a malformed json. That might be the case ...
Probably the error is related to the headers of the response. You are specifying a json datatype, thus the expected headers should be of type application/json
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.