无法读取数据,因为它的格式不正确

时间:2015-12-30 07:04:31

标签: php swift afnetworking

好好经过多次搜索后,我不知道这是怎么回事,因为当我更换钥匙时,我认为我已经修好了。然而,它不再工作,我没有改变任何东西。请勿报告重复,否则没有任何问题得到解答。

    let URL = "http://localhost/donate/payment.php"
    let params: Dictionary<String,AnyObject> = ["stripeToken": token.tokenId,
        "amount": Int(self.amountTextField.text!)!,
        "currency": "usd",
        "description": self.emailTextField.text!]



    let manager = AFHTTPSessionManager()
    manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/plain", "text/html", "application/json"]) as! Set<String>

    manager.PATCH(URL, parameters: params, success: { (operation, responseObject) -> Void in
        guard let response = responseObject as? [String: String] else {
            print("failed")
            return
        } 
        }) { (operation, error) -> Void in

            self.handleError(error)
           }

这是我从appcoda获得的PHP代码(我从未用PHP编写)

    <?php
        require_once('vendor/autoload.php');
        \Stripe\Stripe::setApiKey("key");
        $token =  $_POST['stripeToken'];
        $amount = $_POST['amount'];
        $currency = $_POST['currency'];
        $description = $_POST['description'];
            try {
            $charge = \Stripe\Charge::create(array(

            "amount" => $amount*100, // Convert amount in cents to dollar
            "currency" => $currency,
            "source" => $token,
            "description" => $description)
            );

        // Check that it was paid:
        if ($charge->paid == true) {
            $response = array( 'status'=> 'Success', 'message'=>'Payment has been charged!!' );
        } else { // Charge was not paid!
            $response = array( 'status'=> 'Failure', 'message'=>'Your payment could NOT be processed because the payment system rejected the transaction. You can try again or use another card.' );
     }
      header('Content-Type: application/json');
      echo json_encode($response);

     } catch(\Stripe\Error\Card $e) {
      // The card has been declined
     }
    ?>

1 个答案:

答案 0 :(得分:0)

  如果您要更新现有资源,则使用

PATCH 方法。

  

POST 将要处理的数据(例如,从HTML表单)提交到标识的资源。数据包含在请求正文中。   这可能会导致创建新资源或更新   现有资源或两者兼而有之。

在您的php文件中,您使用POST方法获取通过请求传递的参数,而您使用PATCH方法传递参数,这就是您的服务器返回错误的原因在respnonse