我的xmlhttprequest有什么问题,没有得到任何回复

时间:2016-10-16 08:06:05

标签: javascript php xmlhttprequest



 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
 
 <form action="" method="GET" id="st_epic">                
      <label> EPIC Number</label>
      <input type="text" id="epic_num" name="epic_num" placeholder="Enter the epic number">
 </form>

 <script>
      jQuery(document).ready(function($) {
           $('#st_epic').submit(function() {
                var xhr;
                if (window.XMLHttpRequest) {
                     xhr = new XMLHttpRequest();
                    }
                else {
                     xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }

                var url = '/curl.php';

                xhr.open('GET', url, true);
                xhr.onreadystatechange = function() {
                     if (xhr.readyState===4 && xhr.status===200) {
                          console.log("complte =" + xhr.responseText);
                          var div = document.getElementById('epic_result');
                          div.innerHTML = xhr.responseText;
                     }
                }
                xhr.send();
                return false; 
           })
      })
 </script>

<div id="epic_result">
</div>
 <?php
 $en = isset($_GET['epic_num']);
 $es = isset($_GET['epic_state']);
 $ec = isset($_GET['captcha']);
	
 $url = 'example.com/';
	
 $ch = curl_init();
 
 $fields = 'Search?epic_no=' . $en . '&page_no=1&results_per_page=10&reureureired=a32412c7-5598-401f-903c-f68a8690d4b1;
 
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_POST, true);
	
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
 curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded","Accept: */*"));
 $result = curl_exec($ch);
 curl_close($ch);
 ?>
&#13;
&#13;
&#13;

我正在尝试将来自 mydomain.com 的请求发送到域 example.com / ,其中包含$ fields中的参数。当我跑,我没有得到任何回应,也没有显示一些错误。我很困惑我做错了什么?如果有人可以提供帮助,我们将非常感激。谢谢

//我的curl.php

 

3 个答案:

答案 0 :(得分:0)

如果你包含jQuery,你应该只使用它来获取ajax请求,因为它有一个很好的基于api的承诺。

这个例子使用的是$.get,它是一个&#39; GET&#39;的抽象。 xhr请求。

&#13;
&#13;
jQuery(function($) {
  $('#st_epic').submit(function(e) {
    e.preventDefault()
    $.get('/curl.php')
    .then(function(response){
       console.log("complte =" + response)
        $('#epic_result').html(response)
    })
    .fail(function(err){
      // some error handling
    })
  })
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="GET" id="st_epic">

  <label>EPIC Number</label>
  <input type="text" id="epic_num" name="epic_num" placeholder="Enter the epic number">
  <button type="submit" tabindex="3" id="sub_epic">
    <span class="glyphicon glyphicon-search">Search</span>
  </button>
</form>

<script>
</script>

<div id="epic_result">
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以尝试使用ajax:

jQuery(document).ready(function($) {
    $('#sub_epic').click(function() {
      var epic_num = $("#epic_num").val();
        $.ajax({
            type: "GET",
            url: 'curl.php', 
            data: {epic_num:epic_num}, //if you 
            success: function (data) {
               console.log(data);
               $("#epic_result").html(data); //if data is not object
            }
        });
     });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="st_epic">
    <label> EPIC Number</label>
    <input type="text" id="epic_num" name="epic_num" placeholder="Enter the epic number">
    <button type="button" tabindex="3" id="sub_epic"><span class="glyphicon glyphicon-search"></span>Search</button>
</form>



<div id="epic_result"></div>

答案 2 :(得分:0)

试试这样:

    <script>
           jQuery(document).ready(function($){

             $('#st_epic').submit(function(){
              var epicNum = $("#epic_num").val();
                  $.ajax({
                      url: '/curl.php',
                      method: 'GET',
                      data: {epicNum:epicNum},
                      dataType: 'json'
                  }).done(function (response){
                     if (response.message == 'succes') {
                        console.log("complte =" + response.data);
                        var div = document.getElementById('epic_result');
                       div.html(response.data);
                     }
                  });
            return false;
    })
})
</script>

UPDATE 你有错误的PHP代码((

试试这个:

<?php
  if ($en = isset($_GET['epic_num'])) {
       $es = isset($_GET['epic_state']); //you must send it from ajax
       $ec = isset($_GET['captcha']); // this too
       $url = 'example.com/';
       $ch = curl_init();
       $fields = 'Search?epic_no=' . $en . '&page_no=1&results_per_page=10&reureureired=a32412c7-5598-401f-903c-f68a8690d4b1';
       $url = $url.$fields
       curl_setopt($ch,CURLOPT_URL,$url);
       curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
       $output=curl_exec($ch);
       curl_close($ch);
       return array(
          'message' => 'succes',
          'data' => $output);

  }
?>