简体   繁体   中英

Can't send parameter through alamofire to webservice

I want to send parameter through Alamofire to webservice. I have checked the webservice and it is working properly but it is not accepting parameter just getting response of else part.

Here is my Alamofire code I am using in swift iOS.

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)
})

Here is the code of 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 ) );
    }

   ?>

Issue

Well based from the Alamofire docs if you don't specify HTTPMethod :

The Alamofire.request method parameter defaults to .get .

So judging on that, I think the request being sent there is .get , thus it has no parameters attached to it.

Solution

Change your code to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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