简体   繁体   English

无法获得Ajax请求以json在jQuery中工作

[英]Can't get ajax request to work in jquery with json

This is for the socket.io chat site called cytu.be. 这是用于名为cytu.be的socket.io聊天站点的。 I know the msg code is correct. 我知道味精代码是正确的。 I guess ajax isn't working here for some reason though. 我猜由于某种原因,ajax在这里无法正常工作。 I found this cors-server link from some angularjs code on github thinking it might work. 我从github上的一些angularjs代码中找到了这个cors-server链接,认为它可能有效。 Any possibility of making this link work? 是否有可能使此链接正常工作? (warning: sorry it has explicit pornographic words in it) (警告:抱歉,其中包含明显的色情字词)

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

If you are setting dataType to be json , you do not need to then JSON.parse() the response, as jQuery will perform the parsing for you. 如果将dataType设置为json ,则不需要JSON.parse()响应,因为jQuery将为您执行解析。 In your case, that means you would need to change this line: 就您而言,这意味着您需要更改以下行:

Data = JSON.parse(data.responseText), // note you also have typo here as this should be semicolon at end of line not a comma

to this: 对此:

Data = data.responseText;

It is hard to say if you have any other problems (ie CORS-related issues) as you did not post any debugging info in your question. 很难说是否还有其他问题(即与CORS相关的问题),因为您没有在问题中发布任何调试信息。 These issues should definitely be fixed though. 这些问题绝对应该解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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