从jQuery GET请求返回HTML [英] Return HTML from jQuery GET request

查看:606
本文介绍了从jQuery GET请求返回HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从jQuery触发的GET请求返回HTML表单,但是我不确定如何执行此操作.客户的服务器将呼叫我的,然后将返回一个表单.我知道相同的域策略也会起作用,并且围绕它可以使用多种方法,例如JSONP,但是我不知道如何返回整个HTML表单.

I'm attempting to return an HTML form from a GET request triggered by jQuery but I'm not sure how to go about doing this. A customer's server will call to mine which will then return a form. I understand the same domain policy comes into play and there are ways around it such as JSONP, but I can't figure out how to return an entire HTML form.

现在我正在localhost上进行测试,因此我可以使用GET到服务器上的另一个页面,而不必担心相同的域策略(如果这样做可以简化操作)

这是我的思考过程.

//form_deliver.php
//this code would go on the customer's server which would then call to mine
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<?php
    echo "look at all of this stuff!";
?>
<script> 
    $.getJSON("/form_deliverer.php?form_request=true",function(data) {
            //alert(data.check); //this was just for testing, 
    });
</script>

这是将GET请求发送到的页面

Here's the page that it sends the GET request to

//form_deliverer.php
//this code is on my server which will return a form to the customer
<?
if(isset($_GET['form_request'])){
   // echo json_encode(array("check" => "true")); //just for testing
    //return form here
?>

为简单起见,假设这是我要返回的表格

For simplicity, let's say this is the form I want to return is

<form method="post">
    <input type="textbox" name="text"/>
    <input type="submit" name="submit_text"/>
</form>

根据jackJoe的建议编辑这是我的更新

form_deliver.php上的代码

Code on form_deliver.php

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<?php
    echo "look at all of this stuff";
?>
<script> 
    var form_request = "true";
    $.get("/form_deliverer.php", {var: form_request},
        function(data){
        $("#yourdiv").html(data);
    }, "html");
</script>
<div id = "yourdiv">

</div>

这是在form_deliverer.php上

This is on form_deliverer.php

<?
if(isset($_GET['form_request'])){
    return "
    <form method='post'>
        <input type='textbox' name='text'/>
        <input type='submit' name='submit_text'/>
    </form>
    ";
}

?>

推荐答案

正如我在评论中所说,您可以使用AJAX,也可以使用GET(

As I said in my comment, you could use AJAX or in your case GET (http://api.jquery.com/jQuery.get/) and it allows you to return html.

示例:

$.get("yourPHP.php", {var: somethingdynamicpassedviajavascript},
     function(data){
     //get the result
     $("#yourdiv").html(data);
}, "html");

此函数的结尾具有返回类型(在本例中为"html").此示例将HTML放入div #yourdiv

The end of this function has the return type (in this case "html"). This example would place the HTML into the div #yourdiv

这篇关于从jQuery GET请求返回HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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