首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从iOS向php web服务发送请求

问从iOS向php web服务发送请求
EN

Stack Overflow用户
提问于 2015-12-14 14:35:45
回答 2查看 637关注 0票数 0

我有一个问题,就是从一个iOS应用程序发送一个POST请求到一个用php编写的web服务,这个服务最终会查询一个MySQL数据库。

tldr:如何直接在浏览器窗口中查看POST变量的内容,而无需刷新?

长篇版本:

我用Xcode编写了Swift代码,使用了NSURLSession、请求、数据等。

我将php网页设置为var_dump($_POST);,以便检查数据是否被正确发送(我的数据是Xcode中用于测试的硬编码字符串)。

在我决定将一个测试查询语句添加到我的网页绑定POST变量之前,我一直无法弄明白为什么我总是得到空的POST变量。瞧,查询成功了,我的表也更新了。

我现在意识到,我认为POST变量为空的原因是,为了查看var_dump的结果,我刷新了网页。我现在还知道这是在删除POST数据,因为当我用查询语句重复这个操作时,我的表将得到NULL行。

我的问题是如何直接在浏览器窗口中查看POST变量的内容,而不需要刷新?我知道这一定是我自己做过的一件好事.但我是个菜鸟。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-14 14:43:35

您需要修改服务本身,以某种方式输出这些值。如果这完全是为了调试,那么最好将服务写到日志文件中。如果这是请求应用程序调用的一部分,并且需要向用户显示数据,服务可能会返回应用程序可以解析的XML或JSON字符串响应。否则,您可以使用Fiddler来监视您的网络流量。

票数 0
EN

Stack Overflow用户

发布于 2015-12-24 09:57:56

当然,超时,刷新页面,只会得到一个空变量。这是我以前用来测试我的代码是否工作的东西:

代码语言:javascript
复制
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    testPost() // this function will test that the code is sending the variable to your server
    return true
}

func testPost() {

    let variableToPost = "someVariable"

    let myUrl = NSURL(string: "http://www.yourserver.com/api/v1.0/post.php")
    let request = NSMutableURLRequest(URL: myUrl!)
    request.HTTPMethod = "POST"
    let postString = "variable=\(variableToPost)"
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
        { data, response, error in

            if error != nil {

                print(error)
                return

            }

            do{

                let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary


                if let parseJSON = json{


                    let result = parseJSON["status"] as? String
                    let message = parseJSON["message"] as? String


                    if result == "Success"{

                        //this should return your variable

                        print(message)


                    }else{

                        // print the message if it failed ie. Missing required field
                        print(message)

                    }

                }//if parse

            }  catch let error as NSError {
                print("error in registering: \(error)")


            } //catch

    }

    task.resume()
}

然后php文件将只检查没有空的post,并以JSON:post.php的形式返回变量

代码语言:javascript
复制
<?php
$postValue = htmlentities($_POST["variable"]);

 if(empty($postValue))
 {
 $returnValue["status"] = "error";
 $returnValue["message"] = "Missing required field";
 echo json_encode($returnValue);
 return;
 } else {

 $returnValue["status"] = "success";
 $returnValue["message"] = "your post value is ".$postValue."";
 echo json_encode($returnValue);
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34269598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档