无法通过alamofire向webservice发送参数 [英] Can't send parameter through alamofire to webservice

查看:123
本文介绍了无法通过alamofire向webservice发送参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

以下是Webservice的代码 - esp.php

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

   ?>


推荐答案

问题



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

Issue

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


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

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

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

将您的代码更改为:

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

这篇关于无法通过alamofire向webservice发送参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆