简体   繁体   English

解析JSON错误(从PhoneGap到Ruby)

[英]Parsing JSON error (from PhoneGap to Ruby)

I'm attempting to send a JSON string from PhoneGap to my Ruby application. 我正在尝试从PhoneGap向我的Ruby应用程序发送JSON字符串。 In PhoneGap, the following code is sending the JSON string: 在PhoneGap中,以下代码正在发送JSON字符串:

            var location = {
                lat: position.coords.latitude,
                lng: position.coords.longitude
            }

            $.ajax({
              type: "post",
              dataType: 'json',
              data: location,
              url: 'http://localhost:3000/location/new'
            });

In my Ruby app, I have a controller trying to process the response: 在我的Ruby应用程序中,我有一个控制器试图处理响应:

def addlocation

    require 'json'
    json_string = params[:data]

    parsed_json = JSON.parse(json_string.to_json)

    if parsed_json["app"] == "Inrix PhoneGap"
        new_obj = Location.new
        new_obj.lat = parsed_json["lat"].to_f
        new_obj.lng = parsed_json["lng"].to_f
        new_obj.save
    end

end

I know I'm getting the data from PhoneGap, because the parameters show up in the server log. 我知道我正在从PhoneGap获取数据,因为参数显示在服务器日志中。 I tried converting the response to a string, and then to json, because it doesn't appear to be in the proper json format. 我尝试将响应转换为字符串,然后转换为json,因为它似乎没有采用正确的json格式。 I've also tried passing the JSON.parse function response.body and params[:data]. 我也尝试过传递JSON.parse函数response.body和params [:data]。 Here is the error I receive in the server log: 这是我在服务器日志中收到的错误:

Started POST "/location/new" for 127.0.0.1 at 2013-07-23 01:14:37 -0700
Processing by LocationsController#addlocation as HTML
  Parameters: {"lat"=>"37.785834", "lng"=>"-122.406417"}
Completed 500 Internal Server Error in 0ms

JSON::ParserError (757: unexpected token at 'null'):
  app/controllers/locations_controller.rb:12:in `addlocation'


  Rendered /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
  Rendered /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.9ms)
  Rendered /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (7.5ms)

Thanks in advance. 提前致谢。 Let me know if you need more information. 如果您需要更多信息,请与我们联系。 Cheers! 干杯!

data in $.ajax is not the name of the POST parameter. $.ajax data不是POST参数的名称。 You need to give data: { data: location } in order to have a parameter named data (which would translate to POST data={"lat"=>"37.785834", "lng"=>"-122.406417"} ) 您需要提供data: { data: location }才能有一个名为data的参数(它将转换为POST data={"lat"=>"37.785834", "lng"=>"-122.406417"}

However, I would suggest you name it something like location instead :P 但是,我建议您将其命名为location而不是:P

$.ajax({
  type: "post",
  dataType: 'json',
  data: { location: location },
  url: 'http://localhost:3000/location/new'
});

and

location_json = params[:location]

just so you understand what you wrote six months from now :D 只是为了让您了解从现在开始六个月后写的内容:D

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

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