首页 \ 问答 \ 从AJAX POST获取JSON数据的问题(Issues with getting JSON data from AJAX POST)

从AJAX POST获取JSON数据的问题(Issues with getting JSON data from AJAX POST)

AJAX调用如下所示:

        json_dictionary = {url : url, profile_dict: dictionary, final_dict : final_dictionary}
        $.ajax({
            url: "https://localhost:8090/add_profile_data",
            type: "POST",
            data: JSON.stringify(json_dictionary),
            contentType:'application/json',
            success:function() {
                console.log('added profile data')
            }
        })

客户端看起来像这样:

@app.route('/add_profile_data', methods=['POST', 'GET', 'OPTIONS'])
def add():
    data = request.json
    print(type(data))

这是有效的,打印的结果是<type 'dict'>

此外,当我打印data对象时,一切都在那里。

但是,当我尝试:

@app.route('/add_profile_data', methods=['POST', 'GET', 'OPTIONS'])
def add():
    data = request.json
    print(data.keys())

我收到一个错误: AttributeError: 'NoneType' object has no attribute 'keys'

我不明白为什么会这样? 有什么想法吗?

更新:

将服务器更改为此,现在看到NoneType用于print(type(data))服务器:

@app.route('/add_profile_data', methods=['POST', 'GET', 'OPTIONS'])
def add():
    data = request.json
    print(type(data))
    print(data.keys())

响应:

<type 'NoneType'>
127.0.0.1 - - [04/Jan/2017 09:13:47] "OPTIONS /add_profile_data HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/flask/app.py", line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Library/Python/2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Python/2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/morganallen/Dropbox/Coding_Projects/Prediction_Server/prediction_v2.py", line 172, in add
    print(data.keys())
AttributeError: 'NoneType' object has no attribute 'keys'

我看到NoneType原因是NoneType


The AJAX call looks like this:

        json_dictionary = {url : url, profile_dict: dictionary, final_dict : final_dictionary}
        $.ajax({
            url: "https://localhost:8090/add_profile_data",
            type: "POST",
            data: JSON.stringify(json_dictionary),
            contentType:'application/json',
            success:function() {
                console.log('added profile data')
            }
        })

The client side looks like this:

@app.route('/add_profile_data', methods=['POST', 'GET', 'OPTIONS'])
def add():
    data = request.json
    print(type(data))

That works, and the result from print is <type 'dict'>

Also, when I print the data object, everything is there.

However, when I try:

@app.route('/add_profile_data', methods=['POST', 'GET', 'OPTIONS'])
def add():
    data = request.json
    print(data.keys())

I get an error: AttributeError: 'NoneType' object has no attribute 'keys'

I don't understand why this is happening? Any thoughts?

Update:

Changed the server to this, and now seeing NoneType for print(type(data)) Server:

@app.route('/add_profile_data', methods=['POST', 'GET', 'OPTIONS'])
def add():
    data = request.json
    print(type(data))
    print(data.keys())

Response:

<type 'NoneType'>
127.0.0.1 - - [04/Jan/2017 09:13:47] "OPTIONS /add_profile_data HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/flask/app.py", line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Library/Python/2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Python/2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/morganallen/Dropbox/Coding_Projects/Prediction_Server/prediction_v2.py", line 172, in add
    print(data.keys())
AttributeError: 'NoneType' object has no attribute 'keys'

Any reason why I am seeing NoneType?


原文:https://stackoverflow.com/questions/41465778
更新时间:2022-02-01 15:02

最满意答案

您如何看待以下内容?

  1. 像往常一样计算cam_poscam_destcam_up
  2. 计算cam_right为cross( cam_poscam_up
  3. 创建一个float camera_time (如果是walking, camera_time += delta_time;
  4. compute offset_factor = sin(camera_time);

然后你可以调用gluLookAt或类似的功能如下。

gluLookAt(cam_pos + cam_right * offset_factor, cam_des + cam_right * offset_factort, cam_up)

这将使相机从右向左摆动。 您可以通过一些调整为cam_up向量添加相同的cam_up


What do you think about the following.

  1. compute cam_pos, cam_dest, cam_up as usual.
  2. compute cam_right as cross (cam_pos, cam_up)
  3. create a float camera_time (if walking, camera_time += delta_time; )
  4. compute offset_factor = sin(camera_time);

Then you can call gluLookAt or similar function as follows.

gluLookAt(cam_pos + cam_right * offset_factor, cam_des + cam_right * offset_factort, cam_up)

This will make the camera swing from right to left. You can add the same for the cam_up vector with some tweaking.

相关问答

更多
  • 我的直觉告诉我你在这里滥用static存储限定符。 由于这是C ++,因此在您第一次调用Camera::GetInput (...)时将初始化lastTime ,这是唯一一次为其分配值。 通常,名为deltaTime的变量应该是自上次摄像机更新以来经过的时间量,而不是自第一次调用Camera::GetInput (...)以来经过的时间。 在类函数中具有此范围的static变量对我来说是错误的。 如果你有这个类的两个实例,它们都将使用在第一次调用Camera::GetInput (...)时建立的lastT ...
  • 我在OpenGL中用gluLookAt得到了同样的问题。 我用我自己的相机课程解决了这个问题: void Camera::ComputeVectors() { Matrix4x4 rotX, rotZ; Quaternion q_x, q_y, q_z; Quaternion q_yx, q_yz; q_x.FromAngleAxis(radians.x, startAxisX); q_y.FromAngleAxis(radians.y, startAxisY); ...
  • 您可以使用CONTROL_AF_REGIONS键在CaptureRequest中设置自动对焦测光区域。 自动曝光和自动白平衡区域也有相应的键。 You can set the autofocus metering region in the CaptureRequest with the CONTROL_AF_REGIONS key. There are corresponding keys for the auto-exposure and auto-white balance regions as we ...
  • 要在文件选择器中提供一致的用户体验,当您通过指定.jpg添加基于图像的文件过滤器时,会添加相机按钮以方便使用。 它无法删除。 To provide a consistent user experience in the file picker, when you add an image based file filter as you have done by specifying .jpg, the camera button is added as a convenience. It cannot b ...
  • 如果你有一个a-scene元素的引用,你可以直接引用它。 let sceneEl = ...; let camera = sceneEl.camera; 摄像头是一个组件,可以驻留在一个a-entity或一个a-camera (专门用于API)。 如果元素是A-Frame原语,则它应该有一个存储所有组件的对象。 查询摄像头的简单方法可能是: let cameraEl = document.querySelector('a-entity[camera]') if (!cameraEl) { ...
  • 试试Google的这个示例项目(或者只是Camera2BasicFragment )。 一般情况下,您应该请求焦点,而不是在CameraCaptureSession.CaptureCallback确定“焦点锁定”状态,而不是捕获图像,最后请求解锁焦点。 Try this sample project from Google (or just Camera2BasicFragment). In general You should request focus, than determine "focus lo ...
  • 确保清单中存在相关权限。 根据谷歌开发: 重要提示:将完全初始化的SurfaceHolder传递给setPreviewDisplay(Surfa ...
  • 您如何看待以下内容? 像往常一样计算cam_pos , cam_dest , cam_up 。 计算cam_right为cross( cam_pos , cam_up ) 创建一个float camera_time (如果是walking, camera_time += delta_time; ) compute offset_factor = sin(camera_time); 然后你可以调用gluLookAt或类似的功能如下。 gluLookAt(cam_pos + cam_right * offset ...
  • 我相信你的问题是你忽略了模特相机姿势。 想象一个立方体漂浮在房间里的立方体。 得到它了? 现在在你的脑海中,绘制一个刚性连接到立方体的笛卡尔坐标系,x轴和z轴在水平面上,y轴指向天花板。 由于相机是在您的示例代码中移动的东西,您可能同样可以选择将立方体的坐标系可视化为刚性连接到房间,如果您愿意的话。 现在,想象相机四处移动,从不同的角度观察立方体。 想象一下,一个独立的笛卡尔坐标系统也与之紧密相连。 它的x轴指向相机的右侧,y轴指向顶部,z轴指向背面(即,当您通过取景器观察时朝向您的脸部) 。 当您在拿着相 ...
  • 在API级别21中添加了Camera2。您的应用程序将在api级别为15-20的设备上崩溃。 使用旧的Camera API。 Camera2 is added in API level 21. Your app will crash on devices with api levels 15-20. Use old Camera API.

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)