向本地主机发送POST请求,数据已接收但未显示在网页上 [英] Sending POST Request to Local Host, Data Being Received But Not Showing Up On Webpage

查看:290
本文介绍了向本地主机发送POST请求,数据已接收但未显示在网页上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几天我一直在这里.我正在从iPhone应用程序向MAMP上的本地主机发送一些数据.数据肯定会发送到服务器并由服务器接收,因为我得到的响应是网页上运行的PHP代码,但未在网页上显示.有什么方法可以使网页显示接收到的令牌?我觉得问题是我的PHP脚本真正真正的基本问题.谢谢.这是代码:

I've been at this for the last couple of days. I'm sending some data from my iPhone app to a localhost on MAMP. The data is definitely being sent to and being received by the server, because the response I'm getting is the PHP code running on the webpage, but it isn't showing up on the webpage. Is there any way to make the webpage display the token being received? I feel that the problem is something really basic with my PHP script. Thanks. Here's the code:

Objective-C:

Objective-C:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://192.168.2.12:8888/hello_world.php"]];
request.HTTPMethod = @"POST";
NSString *body = [NSString stringWithFormat:@"stripeToken=%@", token.tokenId];

NSLog(body);

request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];
NSURLResponse *response;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSLog([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

PHP:

<?php
    echo "Hello, World!";
    print_r($_POST);
?>

NSLog:

2014-06-18 12:40:12.300 PayPhone Prototype[3775:60b] Received token tok_104F7R4h7nUnb2nUO3XoHPyK
2014-06-18 12:40:12.301 PayPhone Prototype[3775:60b] stripeToken=tok_104F7R4h7nUnb2nUO3XoHPyK
2014-06-18 12:40:12.306 PayPhone Prototype[3775:60b] <html>
    <head>
        <title>PHP Test</title>
    </head>
    <body>
    Hello, World!Array
(
    [stripeToken] => tok_104F7R4h7nUnb2nUO3XoHPyK
)
    </body>
</html>

网页:

Hello, World!Array ( )

推荐答案

您的网页显示错误"内容的原因是,当您访问URL时,浏览器通过在地址中写入其URL来发送GET请求酒吧.

The reason why your webpage is showing the "wrong" content is because a browser will send a GET request when you visit a URL by writing its URL in the address bar.

通过以下方式替换您的php:

Replace your php by this:

<?php
  echo "Hello, World!";
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    print_r($_POST);
  } else {
    echo "This was not a POST";
  }
?>

然后您会看到它.

为了让您的浏览器发送帖子,您应该,例如,创建一个将对您的php页面进行POST的表单;或者您可以在JavaScript中执行此操作(类似于从Objective-C中执行的操作).我怀疑这样做会有用.

In order for you to have your browser send a post, you should, e.g., create a form that will do a POST to your php page; or you could do that in JavaScript (similarly to what you are doing from Objective-C). I doubt it would be useful.

因此,看来您的应用正在运行,并且php页面的行为正确.

So, it seems that your app is working and your php page is behaving correctly.

可能您应该通过在浏览器中加载php页面来说明您要尝试执行的操作-如果仅用于测试,则不需要它.您添加的NSLog已经告诉了所有人.

Possibly you should explain what you are trying to do by loading the php page in your browser -- if it is just for testing, then you do not need it. The NSLog you added already tells you all.

这篇关于向本地主机发送POST请求,数据已接收但未显示在网页上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆