无法通过alamofire向webservice发送参数

时间:2016-10-28 09:37:50

标签: php ios swift web-services

我想通过Alamofire将参数发送到webservice。我已检查过webservice并且它正常工作但它不接受参数只是得到else部分的响应。

这是我在swift iOS中使用的Alamofire代码。

let parameters: Parameters = [

    "signers": "ram,Rahim",
    "message": "hello,World",
    "path": "www.webservice.com",
    "sequence":"1,2"
]

Alamofire.request("http://www.webservice.pixcel.com/esp.php", parameters: parameters).responseJSON(completionHandler: { (response) in
    print(response)
    print(response.result)
})

以下是Webservice的代码 - esp.php

  <?php
  //Turn off all error reporting
  error_reporting(0);

  if( $_SERVER['REQUEST_METHOD']=='POST' && $_POST['signers'] &&  $_POST['message'] && $_POST['path'] && $_POST['sequence']){

   $path_s = $_POST['path'];
   $path_array = explode(',', $path_s);


   $signer_s = $_POST['signers'];
   $signer_array = explode(',', $signer_s);

   $message_s =$_POST['message'];
   $message_array = explode(',', $message_s);

   $sequence_s =$_POST['sequence'];
   $sequence_array = explode(',',  $sequence_s);    

for ($i = 0; $i < count($signer_array); $i++) {


    $signer = ($signer_array[$i]);
    $path = ($path_array[0]);
    $message = ($message_array[0]);
    $sequence =($sequence_array[$i]);


    $con = mysql_connect('pixcel.pixcelinfo.com','user_pixcel','abc123') or die('Cannot connect to the DB');
    mysql_select_db('easy_sign',$con);


          $query = mysql_query("INSERT INTO `path` (url,name,signer,sequence,message,flag)
          VALUES ('".$path."','".$name."','".$signer."','".$sequence."','".$message."','false')");
   }
      if($query){
      // echo "Data inserted";
    $return['url'] = 'true';
    $return['data'] = "Data inserted";

       header('Content-type: application/json');
exit( json_encode( $return ) );
       }


    } else {
        $return['status']='false';
        $return['message']='Data not inserted!';
        $return['line']=__LINE__;

    header('Content-type: application/json');
exit( json_encode( $return ) );
    }

   ?>

1 个答案:

答案 0 :(得分:2)

问题

如果您没有指定HTTPMethod,则基于Alamofire docs

  

Alamofire.request方法参数默认为.get

因此判断,我认为发送的请求是.get,因此它没有附加参数。

解决方案

将您的代码更改为:

Alamofire.request("http://www.webservice.pixcel.com/esp.php", method: .post, parameters: parameters).responseJSON(completionHandler: { (response) in
    print(response)
    print(response.result)
})