Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对web服务的ajax php请求_Php_Ajax_Web Services - Fatal编程技术网

对web服务的ajax php请求

对web服务的ajax php请求,php,ajax,web-services,Php,Ajax,Web Services,我需要使用ajax编写一个php代码,以获得Web服务器的响应。下面是将发布请求并获得响应的代码。因为这是一个post请求,所以站点会被刷新。没有刷新,我需要响应。我不知道如何在ajax中实现。请帮帮我。非常感谢 <?php if (isset($_POST['Fahrenheit'])&&$_POST['Fahrenheit']!=null) { $out = print_name($_POST['Fahrenheit']); }

我需要使用ajax编写一个php代码,以获得Web服务器的响应。下面是将发布请求并获得响应的代码。因为这是一个post请求,所以站点会被刷新。没有刷新,我需要响应。我不知道如何在ajax中实现。请帮帮我。非常感谢

    <?php
    if (isset($_POST['Fahrenheit'])&&$_POST['Fahrenheit']!=null) {
    $out = print_name($_POST['Fahrenheit']);
    }
    else {
     print_form();
    }

    function print_name($name) {
        $ch = curl_init();
        $far = 'Fahrenheit='.$name;
        curl_setopt($ch, CURLOPT_URL,"http://www.w3schools.com/webservices/tempconvert.asmx     /FahrenheitToCelsius");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$far);

        // http_build_query(array('postvar1' => 'value1')));

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $server_output = curl_exec ($ch);
        curl_close ($ch);
        echo 'It is: '.$server_output;
        return $server_output;
    }


    function print_form() {
      echo '
        <form method="post">
    <table>
      <tr>
        <td>Fahrenheit to Celsius:</td>
        <td>
        <input class="frmInput" type="text" size="30" name="Fahrenheit">
        </td>
      </tr>
      <tr>
        <td></td>
        <td align="right">
         <input name="cel" type="submit" value="Submit" class="button">
         </td>
      </tr>
    </form>
      ';
    }

因为你的问题有点像“请做我自己的作业”,我的答案指向一个好的网站,那就是你可以做你自己的作业:

您要做的是在javascript中使用xmlHTTPRequest()对象将数据发布到Web服务器并读取响应

上面链接的页面有一个断开的演示链接,下面是正确的链接:

页面的来源非常有趣,例如请考虑这个片段:

function createXMLHttpRequest() {
  try { return new XMLHttpRequest(); } catch(e) {}
  alert("XMLHttpRequest not supported");
  return null;
}
var xhReq = createXMLHttpRequest();
    xhReq.open("GET", "sumGet.php?figure1=5&figure2=1", false); //here method of sending data and server page where to send to
    xhReq.send(null);
    var serverResponse = xhReq.responseText;
    $("response").innerHTML = serverResponse;

通过这种方式,您需要重新加载页面以获取帖子的值,您可以通过删除if条件打印表单以始终显示它,或者您可以将此文件的PHP部分放到另一个文件中并使用ajax@Akam好的,但我不知道。你能帮我做这件事吗。我不知道如何传递值以及如何获得响应和查看这些文件。感谢您的支持您显然不符合对所问问题有最低限度理解的要求。尝试研究和学习AJAX,因为它并不是真正用来回答像“教我AJAX”这样的请求。