简体   繁体   English

无法同时从PHP发送content-type:text / xml标头从MYSQL获取数据

[英]Can't send content-type: text/xml header from PHP at the same time getting the data from MYSQL

i hope you can cast some light on my problem. 我希望你能对我的问题有所帮助。 I need to do an AJAX / PHP / MYSQL application to display posts and stuff on the page i'm writing. 我需要做一个AJAX / PHP / MYSQL应用程序才能在我正在写的页面上显示帖子和内容。

I only discovered how to do some simple stuff in PHP after taking some mushrooms but that was years ago and now i don't have mushrooms and i'm just stuck! 我只是在吃了一些蘑菇之后才发现如何在PHP中做一些简单的事情,但是那是几年前的事,现在我没有蘑菇了,我只是被卡住了!

So here's the problem: 所以这是问题所在:

i think i need to send a proper "xml" file through php so the ajax part can take it but: when i try to put the header on top of the php it displays this error: 我认为我需要通过php发送适当的“ xml”文件,以便ajax部分可以接受它,但是:当我尝试将标头放在php顶部时,它将显示此错误:

" Extra content at the end of the document " “文档末尾的其他内容”

When i looked at some tutorials people were using the "header" fearlesly to do such stuff as i want to do and no comments suggested that it didn't work. 当我看一些教程时,人们正在恐惧地使用“标题”来做我想做的事情,没有评论表明它不起作用。 so why it doesn't work on my local server? 那为什么它在我的本地服务器上不起作用?

I'm running: 我在跑:

WAMP Apache 2.2.11 PHP 5.3.0 WAMP Apache 2.2.11 PHP 5.3.0

It also doesn't work on a remote server (PHP 5.3.0) :/ 它也不能在远程服务器(PHP 5.3.0)上运行:/

I read all the stuff i could find till 5am and decided to ask you for help for the first time :) 我读到了凌晨5点为止我能找到的所有东西,并决定第一次向您寻求帮助:)

Thank you! 谢谢!

header('content-type: application/xhtml+xml; charset=utf-8'); 
require_once("allyouneed.php");
require_once("bazingablob.php"); 

$category=$_GET["category"];
$post_tags=$_GET["post_tags"];
$language=$_GET["language"];
$author=$_GET["author"];
$posts_per_page=$_GET["posts_per_page"];
$current_page=$_GET["current_page"];
$order=$_GET["order"];
$hard_limit=$_GET["hard_limit"];
$show_hidden=$_GET["show_hidden"];*/

$wypluj="";
$wypluj="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";    

$bazinga_blob = new bazingablob;

if (!$bazinga_blob->connect_to_database()) 

        {   
            $wypluj.="<IsOK>0</IsOK>";
            echo $wypluj;
            exit;
        }

        else

        {

            $wypluj.="<IsOK>jedziem</IsOK>";                                        

        }


$bb_result=$bazinga_blob->get_all_posts($category,$post_tags,$language,$author,$posts_per_page,$current_page,$order,$hard_limit,$show_hidden);

if ($bb_result) //udalo sie cos znalezc w bazie wedlug kryteriow

        {

            $wypluj.="<Pagination>";

                $wypluj.="<CurrentPage>";

                $wypluj.=$bazinga_blob->posts_pagination["current_page"];

                $wypluj.="</CurrentPage>";

                $wypluj.="<LastPage>";

                $wypluj.=$bazinga_blob->posts_pagination["last_page"];

                $wypluj.="</LastPage>";

                $wypluj.="<PostsCount>";

                $wypluj.=$bazinga_blob->posts_pagination["posts_count"];

                $wypluj.="</PostsCount>";

            $wypluj.="</Pagination>";


            $wypluj.="<Posts>";


                    foreach ($bb_result as $item) 

                    {

                        $wypluj.="<Post>";

                        $wypluj.="<PostId>".$item->post_id."</PostId>";
                        $wypluj.="<PostAuthor>".$item->post_author."</PostAuthor>";
                        $wypluj.="<PostLangId>".$item->post_langid."</PostLangId>";
                        $wypluj.="<PostSlug>".$item->post_slug."</PostSlug>";
                        $wypluj.="<PostTitle>".$item->post_title."</PostTitle>";
                        $wypluj.="<PostGreetingPicture>".$item->post_greeting_picture."</PostGreetingPicture>";
                        $wypluj.="<PostGreetingVideo>".$item->post_greeting_video."</PostGreetingVideo>";
                        $wypluj.="<PostGreetingSound>".$item->post_greeting_sound."</PostGreetingSound>";
                        $wypluj.="<PostShort>".$item->post_short."</PostShort>";
                        $wypluj.="<PostBody>".$item->post_body."</PostBody>";
                        $wypluj.="<PostDate>".$item->post_date."</PostDate>";
                        $wypluj.="<PostPublished>".$item->post_published."</PostPublished>";
                        $wypluj.="<PostSticky>".$item->post_sticky."</PostSticky>";
                        $wypluj.="<PostComments>".$item->post_comments."</PostComments>";
                        $wypluj.="<PostProtected>".$item->post_protected."</PostProtected>";
                        $wypluj.="</Post>";

                    }

            $wypluj.="</Posts>";



        }


echo $wypluj;

The error comes from your browser and indicates that your XML is malformed. 该错误来自您的浏览器,表明您的XML格式不正确。

Setting the application/xhtml+xml header tells the browser to process the document as serious XML. 设置application/xhtml+xml标头可以告诉浏览器将文档作为严肃的XML处理。 XML needs to be "well-formed", ie it must not contain any syntax errors. XML必须是“格式正确的”,即它不得包含任何语法错误。 Apparently you do have a syntax error on line 1 at column 73, which makes the browser abort the attempt to process the document. 显然,您确实在第73行的第73列出现语法错误,这使浏览器中止了处理文档的尝试。

For this reason it's a pain to hand-code XML, you should really look into a library that takes care of the well-formedness for you, like PHP's own XMLWriter . 出于这个原因,手工编写XML是一件很麻烦的事 ,您应该真正研究一个像PHP自己的XMLWriter这样的库,该库可以为您提供良好的格式。

I'm honestly not sure what you're trying to do with the header, it's not any Ajax method I've ever been taught. 老实说,我不确定您要使用标头做什么,这不是我曾经教过的任何Ajax方法。 The header method you're doing looks just a few lines short of outputting the XML to a download prompt. 您正在执行的header方法看起来仅比将XML输出到下载提示少了几行。

Here's my favorite way to do AJAX. 这是我最喜欢的AJAX方式。 Simple, understandable, and quick. 简单,易懂且快速。

  1. Include Jquery. 包括Jquery。
  2. Setup your data--whether by form with a Serialize (gets form data into a Javascript Variable) or by just setting some variables as it seems you're doing above. 设置数据-无论是通过带有序列化的表单(将表单数据获取到Javascript变量中)还是通过设置某些变量,就像您在上面所做的那样。
  3. Send via Jquery Ajax to a separate processing page. 通过Jquery Ajax发送到单独的处理页面。 The page will receive the data you setup as a $_REQUEST variable, with the method depending on whether you defined it as a POST or not (defaults to a GET) 该页面将接收设置为$ _REQUEST变量的数据,其方法取决于您是否将其定义为POST(默认为GET)
  4. The processing page --does-- stuff with the REQUEST data and may or may not respond back to the page. 处理页面确实填充了REQUEST数据,并且可能会也可能不会响应该页面。 This is where you can do stuff like update divs, alert that it worked, etc. 在这里您可以执行诸如更新div,提醒其工作等工作。

Here's a great tutorial . 这是一个很棒的教程 Focus on the code under "Hello Ajax, Meet Jquery" 专注于“ Hello Ajax,Meet Jquery”下的代码

If you get yourself any more of those mushrooms, a PHP familiar way to do AJAX is with XAJAX. 如果您能从中获得更多收益,那么XAJAX是PHP熟悉的AJAX制作方法。 It allows you to do asynchronous calls to PHP functions. 它允许您对PHP函数进行异步调用。 Be aware, though, that the forums are not the most english-friendly and documentation is a bit cryptic. 但是请注意,论坛不是最英语友好的,并且文档有点含糊。

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

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