【问题标题】:confusion with jquery , ajax post与 jquery 混淆,ajax post
【发布时间】:2013-05-21 08:25:39
【问题描述】:

我有一个简单的 html 页面,通过使用 jquery/ajax,我试图将一个简单的 json 文件发送到我的 php 服务器。但我的脚本的行为确实令人困惑..

首先我试过了,我在网上找到了什么(其他 SO 问题):

脚本1


var data = {"deviceUUID":"lssfds998", "os":"bb", "pushToken":"l1355436gdfsfdsl"};

$.ajax({
                         type: "POST",
                         url: "http://192.138.4.115/Server_CityInfo/register.php",
                         data: data,
                         contentType: "application/json; charset=utf-8",
                         dataType: "json"
                         }).success(function(response, status, xhr){
                             console.log(response);
                             console.log(status);
                             console.log(xhr);
                        });

Script1,向我的服务器发送一个空(null)的 json 文件。

脚本2


var data = {"deviceUUID":"lssfds998", "os":"bb", "pushToken":"l1355436gdfsfdsl"};

$.ajax({
                         type: "POST",
                         url: "http://192.168.4.113/Server_CityInfo/register.php",
                         data: data,
                         //contentType: "application/json; charset=utf-8",
                         dataType: "json"
                         }).success(function(response, status, xhr){
                             console.log(response);
                             console.log(status);
                             console.log(xhr);
                        });

在 Script2 中,我注释行 contentType: "application/json; charset=utf-8" 结果稍好,在服务器端我得到类似:deviceUUID=lssfds998&os=bb&pushToken=l1355436gdfsfdsl。但仍然不是我需要的 json 格式。

脚本3


var data = '{"deviceUUID":"lssfds998", "os":"bb", "pushToken":"l1355436gdfsfdsl"}';

$.ajax({
                         type: "POST",
                         url: "http://192.168.4.113/Server_CityInfo/register.php",
                         data: data,
                         //contentType: "application/json; charset=utf-8",
                         dataType: "json"
                         }).success(function(response, status, xhr){
                             console.log(response);
                             console.log(status);
                             console.log(xhr);
                        });

在 Script3 中,我的数据变量是一个字符串。如您所见,我将它包装在 ' ' 中。这一次,我确实在我的服务器上正确获取了 json。

但是,即使我评论 dataType: "json" 行,我也能在我的服务器上正确获取 json。那么这里发生了什么?我有一种感觉,我无法将我的数据编码为 json,所以最后我必须手动完成。还是错了吗?我真的需要在我的请求中指定dataTypecontentType 吗?如果我只是让我的数据看起来像一个完美的 json 字符串,例如:'{"deviceUUID":"lssfds998", "os":"bb", "pushToken":"l1355436gdfsfdsl"}' 并在不使用上述参数的情况下发送它,是否正确?

为了实现目的,这是我的服务器脚本的外观:

// We use php://input to get the raw $_POST results.
$json = file_get_contents('php://input');
$json_post = json_decode($json, true);

//creating variables from received json
$deviceUDID = $json_post['deviceUUID'];
$os = $json_post['os'];
$pushToken = $json_post['pushToken'];

【问题讨论】:

  • -1 提出了一个非常好的问题,我花了将近 20 分钟的时间来撰写并表现出非常令人困惑的行为。如果这不是一个应该问的问题,那是什么?
  • wtf 是怎么回事? -2 ?为了什么???你甚至读过这个该死的问题吗??
  • 据我所知,如果你不指定,jQuery 会对请求的内容进行智能猜测。在 JSON 作为字符串的情况下,它可能只是像 text/htmlappication/x-www-form-encoded 这样的东西,因为它没有意识到内容是 JSON。但是,如果这样可以解决您的问题,为什么还要抱怨指定 dataTypecontentType
  • 我不知道我所做的是否正确,否则从长远来看会有任何问题。我已经使用 c#、java、obj-c、python 等完成了 http post 请求,在所有这些请求中,我总是在尝试请求时指定 data/contentType。所以我想,这就是这样做的方法。
  • 是的,如果这是它唯一的工作方式,那么它可能是正确的方式。如果 jQuery 或 PHP 框架无法正确猜出请求的内容,您需要明确告诉他们您要发送的内容。

标签: jquery html ajax post http-post


【解决方案1】:

看看下面的变化。

您的 JSON 数组应如下所示:

    arr = '[
            {
                "deviceUUID": "lssfds998",
                "os": "bb",
                "pushToken": "l1355436gdfsfdsl"


     }
    ]';

$.ajax
({
     type: "POST",
     url: " DOMAIN/Server_CityInfo/register.php",
     data: {data:arr},
     success : function(result)
     {
         // do something
     }

});

对于服务器端:

<?php
$data = $_POST['data'];
$json = json_decode($data);

// do something with JSON ARRAY
?>

希望这会奏效。谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 2015-10-22
    • 2011-10-13
    • 2016-09-25
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多