简体   繁体   English

ASIHTTPRequest-响应在调试窗口中正确,但在浏览器中为Null

[英]ASIHTTPRequest - Response Correct in Debug Window but Null in Browser

I've never used ASIHTTPRequest before, nor have I dealt with posting anything from an iPhone to a web service before. 我以前从未使用过ASIHTTPRequest,也从未处理过将任何东西从iPhone发布到Web服务的问题。 I'd like to eventually save the posted variables to a database, but I am unsure if they are being posted properly at this time. 我最终希望将发布的变量保存到数据库中,但是不确定当前是否正确发布它们。 When I run the following script in Xcode, I see the correct html code with inserted variables logged in the debug window. 当我在Xcode中运行以下脚本时,在调试窗口中看到了带有插入变量的正确html代码。 My concern is that I don't see the same thing when I access the page in the browser. 我担心的是,当我在浏览器中访问页面时,看不到相同的东西。 My guess is this is to be expected, but I'm not completely sure. 我的猜测是这是可以预期的,但我不确定。 Here is what I am doing: 这是我在做什么:

string1 = [[NSString alloc] initWithString:@"Did this"];
string2 = [[NSString alloc] initWithString:@"come through?"];

NSString *urlString = @"http://www.myurl.com/post_test.php";
NSURL *url = [NSURL URLWithString: urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:string1 forKey:@"String1"];
[request setPostValue:string2 forKey:@"String2"];
NSString *response = nil;
[request startSynchronous];

NSError *error = [request error];
if (!error) {
    response = [request responseString];


    NSLog(@"Response GOOD: %@", response);
}
else {
    NSLog(@"Response BAD: %@", error);
}

This prints the html code from the web page with the uploaded variables correctly, but if I go to the page in my browser the php variables are null. 这将正确打印带有已上传变量的网页html代码,但是如果我在浏览器中转到该页面,则php变量为null。 Here is the small php script: 这是小的php脚本:

<?php

    $received = array();
    $received = $_POST;
    $print_received = print_r($received, TRUE);
    echo "<pre>$print_received</pre>";
    $var1 = $received['String1'];
    $var2 = $received['String2'];
    echo "The message received was: $var1 $var2</br>";

?>

I haven't added the mysql connection code yet because I want to make sure this posting business is successful first. 我尚未添加mysql连接代码,因为我想确保此发布业务首先成功。

If you're getting the correct response from the code that you posted, it means your code is working. 如果您从发布的代码中获得正确的响应,则表明您的代码正在运行。 What is probably happening is that you're not using POST when testing with your browser. 可能发生的情况是,在使用浏览器进行测试时,您没有使用POST

Check to make sure $_POST[] actually contains data first . 检查以确保$_POST[] 首先实际包含数据。 If not, presumably you'd want to output a meaningful error. 如果不是,大概是您要输出有意义的错误。 It looks like you're just building a sandbox of sorts to play and get familiar with ASIHTTPRequest, however if your goals get more complex (and your code becomes public facing) you'll want to spend some time reading up on how to sanitize and validate POST/GET variables before using them. 看起来您只是在构建一个沙盒,以玩耍并熟悉ASIHTTPRequest,但是,如果您的目标变得更加复杂(并且您的代码面向公众),您将需要花一些时间来阅读如何进行清理和清理。使用它们之前,请先验证POST/GET变量。

As you mentioned databases in your question, I assume that isn't what you are looking for. 正如您在问题中提到的数据库一样,我认为这不是您要查找的内容。

Look into Memcached . 查看Memcached It allows you to easily store and retrieve variables in your system. 它使您可以轻松地在系统中存储和检索变量。

I've confirmed that the variables are being posted properly by writing the variables to a text file: 通过将变量写入文本文件,我已经确认变量已正确发布:

$file = "received_text.txt";
$fh = fopen( $file, 'a' ) or die("Cannot open file");
fwrite( $fh, $var1 );
fwrite( $fh, ", ");
fwrite( $fh, $var2 );
fwrite( $fh, "\n" );
fclose( $fh );

I can now proceed. 我现在可以继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM