简体   繁体   中英

AngularJS $http.get “Unexpected token …”

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


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!

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM