weixin_33720956 2018-02-13 15:54 采纳率: 0%
浏览 34

从curl转换为ajax

I would like to get some help with my ajax code. I want to move the curl code in the index.html to work with ajax but I don't know how I can convert from curl to ajax code.

Index.html:

<script>
$button.click(function(e) 
{
   e.preventDefault();
   var emptyMail = true;
   var email = $emailInput.val().trim();
   var $emailInput_1 = $("#email").val();    

   $(document).ready(function()  
       $(this).val("SUBMITTING...");
       $("form").submit();

       $.ajax({
         url: "post.php",
         type: 'POST'
        });
    });
</script>

Here is the post.php

<?php
function _curl($url, $post = "", $headers = "")
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_POST, ($post && is_array($post))? 1 :0);
    if ($post && is_array($post))
    {
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
    }
    if ($headers && is_array($headers))
    {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "/dev/null");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "/dev/null");
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $result = curl_exec($ch);
    return $result;
    curl_close($ch);
}
if ($_POST)
{
    _curl("http://example.com/land/formFillReturnV5b.php", array(
    "name"=>"groups_name",
    "email"=>$_POST['email'],
    "phone"=>"",
    "ip"=> $_SERVER['SERVER_ADDR'],
    "reff"=>"",
    "page"=>"Q6",
    "link"=>"https://example.com/meme",
    "notes"=>"",
    "MID"=>"39918",
    "LID"=>"",
    "ARData"=>"meme",
    "cookie"=>""), array("X-NewRelic-ID" => "VQQBVl9aDRABUFJbAQkOUQ==", "X-Requested-With" => "XMLHttpRequest", "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"));
}
?>

I want to make it like this:

<script>
    $button.click(function(e) 
    {
       e.preventDefault();
       var emptyMail = true;
       var email = $emailInput.val().trim();
       var $emailInput_1 = $("#email").val();    

       $(document).ready(function()  
           $(this).val("SUBMITTING...");
           $("form").submit();

           $.ajax({
             url: "http://example.com/land/formFillReturnV5b.php",
             type: 'POST',
             "name"=>"groups_name",
             "email"=>$_POST['email'],
             "phone"=>"",
             "ip"=> $_SERVER['SERVER_ADDR'],
             "reff"=>"",
             "page"=>"Q6",
             "link"=>"https://example.onlinesalespro.com/meme",
             "notes"=>"",
             "MID"=>"39918",
             "LID"=>"",
             "ARData"=>"meme",
             "cookie"=>""), array("X-NewRelic-ID" => "VQQBVl9aDRABUFJbAQkOUQ==",      "X-Requested-With" => "XMLHttpRequest", "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"));
            });
        });
    </script>

When I tried it, it won't work. If you can show me an example how I can use ajax code to send the data to the server that would be great.

  • 写回答

1条回答 默认 最新

  • weixin_33704234 2018-02-13 16:01
    关注

    Used Documentation

    Example code

    <script>
    $button.click(function(e) 
    {
       e.preventDefault();
       var emptyMail = true;
       var email = $emailInput.val().trim();
       var $emailInput_1 = $("#email").val();    
    
       $(document).ready(function()  
           $(this).val("SUBMITTING...");
           $("form").submit();
    
           $.ajax({
             url: "http://example.com/land/formFillReturnV5b.php",
             type: 'POST',
             data: {name: "groups_name", email: "text@email.ru" /* and other parametrs*/ }
             success: function(result) {
              console.log(result);
             }
            });
        });
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题