【问题标题】:Didn't receive post data from alamofire post? help me没有收到来自 alamofire post 的帖子数据?帮我
【发布时间】:2014-10-16 07:10:40
【问题描述】:

服务器收不到post数据,为什么?我的代码有问题,谢谢

 // post data
let parameters : [String: AnyObject] = [
                        "uuid" : "731DCD02-1E54-4D28-94F3-D47B7A9D62F4",
                        "sid" : "b172f2697e717d14134d43f477761382",
                    ]         
 //init request    
 self.request = Alamofire.request(
                        "POST",
                        "http://test.cnjob.com/test/index/test",
                        parameters: parameters,
                        encoding:ParameterEncoding.JSON)
 //response
 self.request?.responseString { (request, response, body, error) in{

        println("---\nrequest--------------------------------->" +
            "\n\n\(request)\nresponse------------------------->" +
            "\n\n\(response)\nbody---------------------------->" +
            "\n\n\(body)\nerror------------------------------->" +
            "\n\n\(error)")

 }

// 服务器代码为

public function test(){

    sk_base::load_sys_class('logs')->add(date("ymd").'filesDATA',var_export($_REQUEST,true));

    echo json_encode(array('data'=>$_REQUEST));
}

// 结果:(数据为空,为什么?)

回复---------------------------------------------- ----->

Optional(<NSHTTPURLResponse: 0x7f845bd63cb0> { URL: http://xxx/test/index/test } { status code: 200, headers {
    Connection = "Keep-Alive";
    "Content-Length" = 22;
    "Content-Type" = "text/html; charset=utf-8";
    Date = "Thu, 16 Oct 2014 06:23:23 GMT";
    "Keep-Alive" = "timeout=5, max=100";
    Server = "Apache/2.4.4 (Win64) PHP/5.4.12";
    "X-Powered-By" = "PHP/5.4.12";
} })

主体---------------------------------------------- ------->

Optional("{**\"data\":[]**}\r\n")

错误------------------------------ --------> 零 服务器

【问题讨论】:

  • 200 Status code 表示响应成功。可能没有此参数的可用数据。
  • 但是服务器没有收到任何数据,我在Alamofire中查看代码,参数都在(JSON DATA)中。
  • 我的 php 代码有问题吗?
  • 只使用 .URL 编码而不是 .JSON
  • @SaurabhPrajapati 这非常适合我。我尝试了所有可能的解决方案,但仅将 .JSON 更改为 .URL 效果不佳。

标签: ios post swift alamofire


【解决方案1】:

您接收的 JSON 数据。

将您的 Content-Type text/html 更改为 application/json

编辑:

我已经解释了当状态码为200 OK,Still data 在下面的答案中显示null 时的可能原因。

xcode NSURLConnection post return 200 but NSData always null?

【讨论】:

  • 请求----------------------------------------- ----------> { URL:XXX/test/index/test, headers: { "Content-Type" = "application/json"; } } Content-Type 是 application/json。
  • 带有“encoding:ParameterEncoding.JSON”的初始化请求所有类型都将由 Alamofire 自动配置
  • 你的意思是说你在请求中传递了application/json。如果是,那么您可以看到您的响应标头显示text/html
  • request--》headers: { "Content-Type" = "application/json"; response-----> Optional( { URL: xxx/test/index/test } { 状态码: 200, headers { Connection = "Keep-Alive"; "Content-Length" = 22; "Content-Type" =“文本/html;字符集=utf-8”;日期=“星期四,2014 年 10 月 16 日 06:23:23 GMT”;“保持活动”=“超时=5,最大值=100”;服务器 =“Apache/ 2.4.4 (Win64) PHP/5.4.12"; "X-Powered-By" = "PHP/5.4.12"; } })
  • 如你所说,没关系,但是打印空:php code“ echo json_encode(array('data'=>$_REQUEST));”
【解决方案2】:

将 .JSON 更改为 .URL 非常适合编码。

我尝试了所有可能的解决方案,但仅将 .JSON 更改为 .URL 效果不佳。谢谢saurabh。

【讨论】:

    【解决方案3】:

    我也有同样的问题,但是当我从 .JSON 编码更改 .URL 编码时,它工作得很好!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      相关资源
      最近更新 更多