0

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 ...

10
  • 1
    Are you sure there isn't a difference between the 2 files? Commented Nov 19, 2012 at 22:07
  • 1
    and what is the error that it returns? Commented Nov 19, 2012 at 22:07
  • @romo im pretty sure that both files have the exact same content. Commented Nov 19, 2012 at 22:08
  • actually no error returns @AbrahamP . I set up alert() for success and error so all I got is just info from alert(). Commented Nov 19, 2012 at 22:09
  • what browser are you working in? Commented Nov 19, 2012 at 22:10

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.