在这个问题上我已经走到了尽头。我在我的应用程序中看似随机的地方收到此错误。 Raven 从几十个位置报告它们,但我只能在本地复制几个。在我看来,问题与解析 JSON 响应有关,但响应是有效的。
在我的 Angular 服务中...
2 3 4 5 6 7 | getThread: function(id, success, error) { $http.get('/message/'+id).success(function(data){ success(data); }).error(error); } ... |
在我的 Express 控制器中...
2 3 | res.json(mssgs); ... |
这是一个示例响应...
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | { "id": 79, "body":"test", "senderArchived": false, "recipientArchived": false, "createdAt":"2014-04-17T01:44:46.762Z", "updatedAt":"2014-04-17T01:44:46.762Z", "RootMessageId": 69, "SenderId": 164050, "RecipientId": 154040, "sender": { "username":"boca", "id": 164050, "primaryMedium": null }, "recipient": { "username":"quimby", "id": 154040, "primaryMedium": { "id":"186", "type":"image", "nativeURL":"https://domain/imageurl.jpg", "mediumURL":"https://domain/imageurl.jpg", "smallURL":"https://domain/imageurl.jpg", "createdAt":"2014-04-21T15:52:10.927Z", "updatedAt":"2014-04-21T15:52:10.947Z", "CommentId": null, "EventId": null, "UserId": 154040, "PostId": null, "MessageId": null, "MediaFolderId": null } }, "messageMedia": [] } ] |
在 Chrome 和 Safari 中,这都会导致错误"Uncaught SyntaxError: Unexpected token )"
这是来自 Chrome 的请求标头...
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | Request URL:https://localhost:3001/message/69 Request Method:GET Status Code:200 OK Request Headers Accept:application/json, text/plain, */* Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8,ja;q=0.6 Connection:keep-alive Cookie:streamLoc=%7B%22distance%22%3A-1%2C%22locName%22%3A%22%22%7D; usePostLocation=yes; connect.sid=s%3AhX37rupUct2jut4yApN1GIH9.n5nPURTMXl5OKd46rMqeRc4bg1Q%2F%2Bky0El2r%2BcBvC8c; user=%7B%22id%22%3A154040%2C%22role%22%3A%7B%22bitMask%22%3A32%2C%22title%22%3A%22admin%22%7D%2C%22username%22%3A%22quimby%22%2C%22emailVerified%22%3Atrue%2C%22verified%22%3Atrue%7D Host:localhost:3001 Referer:https://localhost:3001/messages User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36 Response Headers Connection:keep-alive Content-Length:1115 Content-Type:application/json; charset=utf-8 Date:Tue, 29 Apr 2014 02:41:47 GMT ETag:"485872145" X-Powered-By:Express |
所有其他类似问题都指向 JSONP 或实际语法问题,但我没有使用 JSONP,也没有任何代码语法问题。
鉴于此,看起来您不需要使用任何解析功能,但缺少一些东西。
如果你不能总是重现它,我会启用记录网络调用,这样你就可以看到准确地检索出哪些响应会带来麻烦。
由于我不知道您的具体情况,所以无法提供确切的答案。但是,这是我的尝试。
我看到您的服务没有返回任何内容,而是在其中处理成功和错误函数。
2 3 4 5 | $http.get('/message/'+id).success(function(data){ success(data); }).error(error); } |
你的错误(错误)是正确的吗?我假设您将错误函数作为参数传递,但我发现
之间存在不一致
这是建议。我认为你应该改变你的服务只返回Promise,而不是处理任何事情。所以,它应该看起来像这样。
2 3 | return $http.get('/message/'+id); } |
然后你的控制器处理成功和错误,这样你就可以看到所有的成功信息和错误信息。因此,控制器将具有如下代码
2 3 4 | function(data) {....}, function(error) {....} } |
在您的控制器中拥有数据处理逻辑会给您的调试带来更大的困难。
我建议您在控制器中处理 http 数据,而不是在服务中。
你是否在
中传递了一个 null 作为 id
2 3 4 5 | $http.get('/message/'+id).success(function(data){ success(data); }).error(error); } |
解析 $http.get('/message/' )
时会出现语法错误
在 $http 调用之前插入一个 null 测试并等待它被命中。
是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
我有一个非常简单的RubyRack服务器,例如:app=Proc.newdo|env|req=Rack::Request.new(env).paramspreq.inspect[200,{'Content-Type'=>'text/plain'},['Somebody']]endRack::Handler::Thin.run(app,:Port=>4001,:threaded=>true)每当我使用JSON对象向服务器发送POSTHTTP请求时:{"session":{"accountId":String,"callId":String,"from":Object,"headers":
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
Rails中有没有一种方法可以提取与路由关联的HTTP动词?例如,给定这样的路线:将“users”匹配到:“users#show”,通过:[:get,:post]我能实现这样的目标吗?users_path.respond_to?(:get)(显然#respond_to不是正确的方法)我最接近的是通过执行以下操作,但它似乎并不令人满意。Rails.application.routes.routes.named_routes["users"].constraints[:request_method]#=>/^GET$/对于上下文,我有一个设置cookie然后执行redirect_to:ba
我正在使用ruby2.1.0我有一个json文件。例如:test.json{"item":[{"apple":1},{"banana":2}]}用YAML.load加载这个文件安全吗?YAML.load(File.read('test.json'))我正在尝试加载一个json或yaml格式的文件。 最佳答案 YAML可以加载JSONYAML.load('{"something":"test","other":4}')=>{"something"=>"test","other"=>4}JSON将无法加载YAML。JSON.load("
我正在使用Heroku(heroku.com)来部署我的Rails应用程序,并且正在构建一个iPhone客户端来与之交互。我的目的是将手机的唯一设备标识符作为HTTPheader传递给应用程序以进行身份验证。当我在本地测试时,我的header通过得很好,但在Heroku上它似乎去掉了我的自定义header。我用ruby脚本验证:url=URI.parse('http://#{myapp}.heroku.com/')#url=URI.parse('http://localhost:3000/')req=Net::HTTP::Post.new(url.path)#boguspara
我试图在我的网站上实现使用Facebook登录功能,但在尝试从Facebook取回访问token时遇到障碍。这是我的代码:ifparams[:error_reason]=="user_denied"thenflash[:error]="TologinwithFacebook,youmustclick'Allow'toletthesiteaccessyourinformation"redirect_to:loginelsifparams[:code]thentoken_uri=URI.parse("https://graph.facebook.com/oauth/access_token
我是Ruby的新手。我试过查看在线文档,但没有找到任何有效的方法。我想在以下HTTP请求botget_response()和get()中包含一个用户代理。有人可以指出我正确的方向吗?#PreliminarycheckthatProggitisupcheck=Net::HTTP.get_response(URI.parse(proggit_url))ifcheck.code!="200"puts"ErrorcontactingProggit"returnend#Attempttogetthejsonresponse=Net::HTTP.get(URI.parse(proggit_url)