如何从响应中获取URL?

时间:2014-07-03 02:39:40

标签: jquery

首先,我们说我得到了这段代码:

 // Put the results in a div
  posting.done(function( data ) {
    var content = $( data ).find( "#content" );
    $( "#result" ).empty().append( content );
  });

对象数据从该帖子请求返回什么?我想从该帖子请求中获取响应URL,是否可能?

这是我的代码:

<form id="myForm" name="login" action="http://Xsite.com/login" method="post">
    <input type="text" id="TID" name="TID" />
    <input type="password" id="TClave" name="TClave" />
    <input type="submit" value="Login" />
</form>

<script>
    $(".myForm").submit(function(event) {

      event.preventDefault();

      var $form = $( this ),
          e = $form.find( 'input[name="TID"]' ).val(),
          p = $form.find( 'input[name="TClave"]' ).val(),
          url = $form.attr( 'action' );

      var posting = $.post( url, { TID: e,TClave: p } );

      posting.done(function( data ) {
       // how to get the url for testing here?
       alert('data'); // what does this return?
      });
    });
</script>

1 个答案:

答案 0 :(得分:0)

尝试这种方式提交表单并将post.php文件中的响应发送到ajax成功或完成函数

//script
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"> </script>
<form id="myForm" name="login" action="post.php" method="post">
  <input type="text" id="TID" name="TID" />
  <input type="password" id="TClave" name="TClave" />
  <input type="submit" value="Login" />
</form>

<script type="text/javascript">
$("#myForm").submit(function(e)
{
    var postData = $(this).serializeArray();
    var formURL = $(this).attr("action");
    $.ajax(
    {
        url : formURL,
        type: "POST",
        data : postData,
        dataType:'text',
        done:function(data, textStatus, jqXHR)
        {
             //if done , you can also use success here
            console.log(data);
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            //if fails  
            console.log(textStatus);  
        }
    });
    e.preventDefault(); //STOP default action

});
</script> 

//post.php
<?php
print_r($_POST); //your posted data
echo "Xsite.com/index?Error=-2"; //send url when credentials are not valid
die;
?>