1

I try to get a string from a .txt file from another server.

When I run the request, the console shows me this error:

"Unexpected token /" The / is a placeholder, the error contains the first letter of the string on the server.

My code:

var url = "http://r0bs.net/ihdccjsonapi.php?url="+encodeURIComponent(content);
$http.get(url)
  .success(function (data) {
    console.log(data);
  })

When you navigate manual to the url you see this:

Familie. Beruf. Freizeit. Egal, was Sie vorhaben, mit dem Golf Variant machen Sie immer eine sportliche Figur. Entdecken Sie das Auto für einen aktiven Lebensstil.

The Error for this url is "unexpected token F"

I can't post the url here, i hope you can help me.

Varha

4
  • Have you access to the code on server, if yes show how it is handling the request? On which console you see the error, client or server side? What is the encoding of your text file, are you reading it with correct encoding? Show the result of encodeURIComponent(content). Commented Feb 23, 2015 at 14:34
  • @UlukBiy i can access and read JSON and images, plain text too Commented Feb 23, 2015 at 14:41
  • Have you tried to add text/plain to your $httpProvider.defaults.headers.common ? Commented Feb 23, 2015 at 15:20
  • @Iborgav how? i didn't try this Commented Mar 2, 2015 at 13:03

2 Answers 2

8


You can try to set a magical "transformResponse" property in your $http request.
Here is an example:

$http({
    method: 'POST',
    url: '/some/url',
    data: {},
    transformResponse: function (data, headersGetter, status) {
        //This was implemented since the REST service is returning a plain/text response
        //and angularJS $http module can't parse the response like that.
        return {data: data};
    }
}).success(function () {
    //Some success function
}).error(function () {
    //Some error function
});

For sure, you can always modify the response as you need it. :)

Cheers!

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

1 Comment

This is the solution. Should be accepted as an answer
4

The problem is with invalid JSON you are returning from the server. I’ve just met with the same problem.

Looking at the source of Angular, I found a toJSON and fromJSON functions in @module ng that are trying to JSON.parse() given value if it is a string, which in your case seems to be true.

It should be easily solved by serializing data server-side, so Angular could parse it without any problem.

Good luck.

2 Comments

How do you return the value? Show us the server response.

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.