Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从iOS向php web服务发送请求_Php_Ios_Mysql_Database_Swift - Fatal编程技术网

从iOS向php web服务发送请求

从iOS向php web服务发送请求,php,ios,mysql,database,swift,Php,Ios,Mysql,Database,Swift,我有一个问题与从iOS应用程序向用php编写的web服务发送POST请求有关,该web服务最终将查询MySQL数据库 tldr:如何在不刷新的情况下直接在浏览器窗口中查看POST变量的内容? 长版本: 我已经用Xcode编写了Swift代码,包括NSURLSession、request、data等 我将一个php网页设置为var\u dump($\u POST)以便检查数据是否正确发送(出于测试目的,我的数据是Xcode中的硬编码字符串) 在我决定向绑定POST变量的网页添加一条测试查询语句之前

我有一个问题与从iOS应用程序向用php编写的web服务发送POST请求有关,该web服务最终将查询MySQL数据库

tldr:如何在不刷新的情况下直接在浏览器窗口中查看POST变量的内容?

长版本:

我已经用Xcode编写了Swift代码,包括NSURLSession、request、data等

我将一个php网页设置为
var\u dump($\u POST)
以便检查数据是否正确发送(出于测试目的,我的数据是Xcode中的硬编码字符串)

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

我现在意识到,我之所以认为
POST
变量为空,是因为我正在刷新网页,以便查看
var\u dump
的结果。我现在还知道这是在删除
POST
数据,因为当我对查询语句重复此操作时,我的表得到了
NULL

我的问题是如何直接在浏览器窗口中查看
POST
变量的内容而不刷新?我知道这一定是一场真正的无趣的追逐,我一直在引导自己。。。但我是个笨蛋


谢谢

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

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

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

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

欢迎来到这里,我们帮助代码有问题的人。如果您的代码有问题(例如错误),请将代码发布到您的问题中,并告诉我们您希望代码做什么,它实际做什么,以及您尝试解决此问题的方法。也看看这个页面这里告诉你也看看那里的页面它告诉你所有你需要知道的。这个问题与mysql、sowft、ios或数据库有什么关系?@BRoebie好的,问题是我的代码实际上是有效的。。。任何地方都没有错误,它实现了我想要的。我只想知道如何在不刷新浏览器窗口的情况下从浏览器窗口访问POST变量。是否尝试在POST后打开浏览器并查看值?您需要存储和检索该值才能执行此操作。是的,这是一项调试任务。谢谢你的回答。