Java 卷曲和形状-不重定向到页面

Java 卷曲和形状-不重定向到页面,java,php,javascript,jquery,curl,Java,Php,Javascript,Jquery,Curl,我有两页: one.php <?php if($_POST && $_POST['captcha'] == 'thisisrandom'){ echo 'KEY = ' . uniqid(); } ?> <form action="one.php" method="POST"> NAME: <input type="text" name="name"> <br /> CAPTCHA: <input ty

我有两页:

one.php

<?php if($_POST && $_POST['captcha'] == 'thisisrandom'){
    echo 'KEY = ' . uniqid();
} ?>
<form action="one.php" method="POST">
    NAME: <input type="text" name="name"> <br />
    CAPTCHA: <input type="text" name="captcha"> <br />

    <input type="submit">
</form>
<?php
function get_web_page($url)
{
        //echo "curl:url<pre>".$url."</pre><BR>";
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 15,      // timeout on connect
        CURLOPT_TIMEOUT        => 15,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects

    );

    $ch      = curl_init($url);
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch,CURLINFO_EFFECTIVE_URL );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;

    //change errmsg here to errno
    if ($errmsg)
    {
        echo "CURL:".$errmsg."<BR>";
    }
    return $content;
}
print_r(get_web_page('http://localhost/one.php'));
?>

名称:
验证码:
two.php

<?php if($_POST && $_POST['captcha'] == 'thisisrandom'){
    echo 'KEY = ' . uniqid();
} ?>
<form action="one.php" method="POST">
    NAME: <input type="text" name="name"> <br />
    CAPTCHA: <input type="text" name="captcha"> <br />

    <input type="submit">
</form>
<?php
function get_web_page($url)
{
        //echo "curl:url<pre>".$url."</pre><BR>";
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 15,      // timeout on connect
        CURLOPT_TIMEOUT        => 15,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects

    );

    $ch      = curl_init($url);
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch,CURLINFO_EFFECTIVE_URL );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;

    //change errmsg here to errno
    if ($errmsg)
    {
        echo "CURL:".$errmsg."<BR>";
    }
    return $content;
}
print_r(get_web_page('http://localhost/one.php'));
?>

您可以通过Jquery进行AJAX Post调用,以防止表单提交时重定向

首先从表单元素中删除操作属性。HTML应如下所示:

$(document).ready(function() {
    $('form input[type=submit]').click(function(event) {
        event.preventDefault();
        var formData = $(this).closest('form').serialize();
        $.ajax({
                type: 'POST',
                url: 'one.php',
                data: formData,
                dataType: 'json',
                success: function(data) {
                   // do your work here
                }
            });
     });
});
作为最后一件事,您需要使用php函数格式化php输出。因此,one.php应该如下所示:

$(document).ready(function() {
    $('form input[type=submit]').click(function(event) {
        event.preventDefault();
        var formData = $(this).closest('form').serialize();
        $.ajax({
                type: 'POST',
                url: 'one.php',
                data: formData,
                dataType: 'json',
                success: function(data) {
                   // do your work here
                }
            });
     });
});


使用
Javascript
Jquery
直接将数据发布到两个服务器。php
通过curl,但Jquery不允许从其他服务器获取数据好吧,这些信息改变了我的答案!使用JSONP检查这个问题:嘿@JoeChroock,我改进了跨域问题的答案,试试看。