2015-02-23 88 views
1

我嘗試從另一臺服務器的.txt文件中獲取一個字符串。

當我運行的要求,控制檯顯示我這個錯誤:

「意外令牌/」 的/是一個佔位符,錯誤包含服務器上字符串的第一個字母。

我的代碼:

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

當您導航手冊,網址,您看到這一點:

FAMILIE。 Beruf。 FREIZEIT。 Egal,是Sie vorhaben,mit dem Golf Variant machen Sie immer eine sportliche Figur。 Entdecken Sie das Autofüreinen aktiven Lebensstil。

此URL錯誤是「意外的標記F」

我不能在這裏文章的網址,我希望你能幫助我。

Varha

+0

你有沒有訪問服務器上的代碼,如果是的話顯示它是如何處理請求?在哪個控制檯上可以看到錯誤,客戶端或服務器端?什麼是你的文本文件的編碼,你是否閱讀正確的編碼?顯示encodeURIComponent(content)的結果。 – 2015-02-23 14:34:59

+0

@UlukBiy我可以訪問並閱讀JSON和圖片,純文本 – Varha 2015-02-23 14:41:52

+0

您是否嘗試將文本/純文本添加到$ httpProvider.defaults.headers.common? – lborgav 2015-02-23 15:20:41

回答

4

問題是您從服務器返回的無效JSON。我剛剛遇到了同樣的問題。

綜觀角的來源,我發現在@module納克toJSONfromJSON函數試圖JSON.parse()給定值,如果它是一個字符串,而你的情況似乎是真實的。

它應該可以通過序列化數據服務器端輕鬆解決,所以Angular可以毫無問題地解析它。

祝你好運。

+0

我之前試過這個......沒有任何變化:( – Varha 2015-03-02 15:11:40

+0

)你如何返回值?向我們展示服務器的響應 – kuka 2015-03-02 16:34:27

+0

'SyntaxError:意外的令牌E 在Object.parse(native) at fromJson (http:// localhost:9000/bower_components/angular/angular.js:1065:14) at httpHttpResponseTransform(http:// localhost:9000/bower_components/angular/angular.js:8579:16) at http: /localhost:9000/bower_components/angular/angular.js:8664:12 ' – Varha 2015-03-04 16:46:23

8


您可以嘗試在$ http請求中設置一個神奇的「transformResponse」屬性。
下面是一個例子:

$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 
}); 

可以肯定的,你可以隨時修改的響應,你需要它。 :)

乾杯!

+0

真棒..非常好的解決方案。它拯救了我的一天。 – 2016-03-29 16:30:26

+0

這是解決方案。應該被接受爲答案 – 2016-10-06 06:09:24

相關問題