gpt4 book ai didi

ios - 获取快速发布变量时出错

转载 作者:行者123 更新时间:2023-11-28 09:18:36 25 4
gpt4 key购买 nike

我已经在 swift ios 中创建了一个 HTTP POST 请求。当我在 php 文件中获取一个 post 变量时,它显示为空白,我没有得到变量值。在我的 swift 代码下面,看一下。

var dataString = "name=john&type=student"

var request : NSMutableURLRequest = NSMutableURLRequest()

request.URL = NSURL(string: "http://www.example.com/example.php")

var postString = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)

request.HTTPMethod = "POST"

request.setValue("application/json", forHTTPHeaderField: "Content-Type")

request.HTTPBody = postString

var connection = NSURLConnection(request: request, delegate: self, startImmediately: false)

func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
//New request so we need to clear the data object
println(response)
}

PHP代码:

<?php
echo $_POST['name'];
?>

任何人都可以建议我应该如何获取变量值。谢谢

最佳答案

您应该使用 application/x-www-form-urlencodedContent-Type 而不是 application/json

关于ios - 获取快速发布变量时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26298793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com