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>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 关于#华为#的问题:华为HMS账号登录
  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?