【问题标题】:Sending a request to a php web service from iOS从 iOS 向 php Web 服务发送请求
【发布时间】:2015-12-14 14:35:45
【问题描述】:

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

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

长版:

我在 Xcode 中编写了我的 Swift 代码,包括 NSURLSession、请求、数据等。

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

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

我现在意识到我认为POST 变量为空的原因是因为我正在刷新网页以查看var_dump 的结果。我现在也知道这是删除 POST 数据,因为当我使用查询语句重复此操作时,我的表正在获取 NULL 行。

我的问题是如何在浏览器窗口中直接查看POST变量的内容,而不需要刷新?我知道这一定是我带领自己进行的真正的菜鸟追逐......但我是菜鸟。

谢谢

【问题讨论】:

  • 欢迎来到 SO,我们在这里帮助那些遇到代码问题的人。如果您的代码有问题(例如错误),请将您的代码发布到您的问题中,并告诉我们您希望代码做什么、它实际做什么以及您为解决此问题所做的尝试。还可以看看这个页面,它告诉你how to ask a good question 也看看tour 页面,它告诉你所有你需要知道的关于SO。
  • 这个问题和mysql、sowft、ios还是数据库有什么关系?
  • @BRoebie 好吧,我的代码确实有效......任何地方都没有错误,它实现了我想要的。我只想知道如何在不刷新浏览器窗口的情况下访问 POST 变量。
  • 您是否尝试在发布后打开浏览器并查看价值?为此,您需要存储和检索该值。

标签: php ios mysql database swift


【解决方案1】:

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

【讨论】:

  • 是的,这是一个调试任务。谢谢你的回答。
【解决方案2】:

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

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 文件将仅检查是否没有空帖子并将变量返回为 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);
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多