我收到了一個錯誤「JSON在位置26的意外令牌」對於webRTC優惠。我正在使用ajax調用從數據庫獲取報價並將其作爲純文本返回以解析爲JSON對象。當我嘗試解析字符串時,我得到了「JSON中的意外標記」錯誤。JSON中的意外令牌JSON.parse()
這裏是JSON作爲一個純字符串
{"type":"offer","sdp":"v=0
o=- 552724588234335198 2 IN IP4 127.0.0.1
s=-
t=0 0
a=msid-semantic: WMS
m=application 52731 DTLS/SCTP 5000
c=IN IP4 192.169.2.55
a=candidate:2144433521 1 udp 2122257663 2002:c0a9:237::c0a9:237 52729 typ host generation 0 network-id 4
a=candidate:1068658286 1 udp 2122189567 2001::9d38:90d7:88a:6fc:52ce:a035 52730 typ host generation 0 network-id 7
a=candidate:398976708 1 udp 2122129151 192.169.2.55 52731 typ host generation 0 network-id 3
a=candidate:827492737 1 tcp 1518277887 2002:c0a9:237::c0a9:237 9 typ host tcptype active generation 0 network-id 4
a=candidate:1899074206 1 tcp 1518209791 2001::9d38:90d7:88a:6fc:52ce:a035 9 typ host tcptype active generation 0 network-id 7
a=candidate:1497635380 1 tcp 1518149375 192.169.2.55 9 typ host tcptype active generation 0 network-id 3
a=ice-ufrag:EHtolesxvVPp2FqI
a=ice-pwd:CrcvrgKTp6lUbUA81nlGeTFr
a=fingerprint:sha-256 01:39:AF:9A:67:87:F9:52:E9:20:3C:0D:4A:8B:A0:22:E2:D1:01:65:51:32:E1:5B:8B:9E:BC:CA:6E:DF:E2:46
a=setup:actpass
a=mid:data
a=sctpmap:5000 webrtc-datachannel 1024
"}
和代碼
$.ajax({
url: "./php/actions.php",
type: "post",
dataType: "text",
data: {type:'retrieve', roomid:roomid},
success: function(data){
//data is plain text from string of offer retrieved from db
$('.termp').text(data); //used to check raw text data for now
var offerSDP = JSON.parse(data); //returning error when trying to convert to json
remoteConn.setRemoteDescription(new RTCSessionDescription(offerSDP)); //needs an object
}
});
我已經在不同的應用程序,我得到的原始文本數據之前,使用複製和粘貼方法取得了成功並將其粘貼到文本區域中,然後粘貼到JSON.parse(text-area.val())併成功設置sessionDescription,但方法看起來是相同的,但會產生不同的結果。
可以更新的問題,以原始JSON字符串中刪除意見,以便我們可以看到串到底是什麼? –
你可以請張貼準確的'json'迴應? –
發佈了全文字符串響應,我用ajax在成功時返回一個字符串,以便我可以在之後解析它。這是我嘗試在數據中返回一個json對象之後,但RTCSessionDescription(data)不起作用,因爲它表示傳遞的參數不是一個對象。 –