这是一个名为 cytu.be 的 socket.io 聊天网站。我知道消息代码是正确的。我想 ajax 由于某种原因不能在这里工作。我从 github 上的一些 angularjs 代码中找到了这个 cors-server 链接,认为它可能有效。是否有可能使该链接有效? (警告:抱歉,其中含有明显的色情词语)
if (msg.indexOf('$add ') === 0) {
term = msg.split('$add ')[1].split(",");
surl = 'http://cors-server.getup.io/url/api.redtube.com/?data=redtube.Videos.searchVideos&search='+term;
$.ajax({url: surl,
dataType: 'json',
success: function(data) {
console.log(data);
Data = JSON.parse(data.responseText),
socket.emit("chatMsg", {
msg: Data.videos[0].video.video_id
});
}
});
}
最佳答案
如果您将 dataType
设置为 json
,则不需要 JSON.parse()
响应,因为 jQuery 会为您执行解析。就您而言,这意味着您需要更改此行:
Data = JSON.parse(data.responseText), // note you also have typo here as this should be semicolon at end of line not a comma
对此:
Data = data.responseText;
很难说您是否还有其他问题(即 CORS 相关问题),因为您没有在问题中发布任何调试信息。不过,这些问题绝对应该得到解决。
关于javascript - 无法让ajax请求在jquery中使用json工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32830936/