BG Development


  Reply to this topicStart new topicStart Poll

> cURL с AJAX, AJAX, PHP, CURL
cpuin
Публикувано на: 11-05-2020, 17:16
Quote Post



Име:
Група: Потребител
Ранг: Почетен член

Мнения: 900
Регистриран на: 06.03.12



Здравейте,


Имам cURL заявка, която има за цел да обнови количества изпращайки данни под формата на array.

Искам да го осъществя с AJAX, но нещо error хандлинг-а ми куца.

CODE

$(document).ready(function() {
         
         var progressElem = $('#progressCounter');
            var URL = "https://site.com/admin/integration/update_qty";
            $("#loading").hide();
            progressElem.text(URL);
         
         $('#btn').click(function() {
             
             $.ajax({
                        type: 'GET',
                        url: URL,
                        cache: false,
                  error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.responseText);
                        alert(thrownError);
               },
                  xhr: function () {
                        var xhr = new window.XMLHttpRequest();
                        //Download progress
                        xhr.addEventListener("progress", function (evt) {
                        console.log(evt.lengthComputable);
           if (evt.lengthComputable) {
               var percentComplete = evt.loaded / evt.total;
               progressElem.html(Math.round(percentComplete * 100) + "%");
           }
       }, false);
       return xhr;
   },
   beforeSend: function () {
       $('#loading').show();
   },
   complete: function () {
       $("#loading").hide();
   },
   success: function (json) {
       $("#data").html("<div class='alert alert-success'><strong>Количествата са обновени успешно!</strong></div>");
   }
});
             
             
   });
   
   
   
});



CODE

$hash = base64_encode('username' . ':' . 'password');
            $requestData = array('data' => $chunk);
            $headers = array('Authorization: Basic ' . $hash);
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, 'https://site2.com/api-3/product_offer/save');
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
                    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestData));
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                    $result = curl_exec($ch);
                    echo $result;
                    if(curl_errno($ch)){
                              echo 'Request Error:' . curl_error($ch);
                              }
                        
            }


--------------------
"Трудните неща изискват време, невъзможните - просто малко повече"
PMEmail Poster
Top
1 потребители преглеждат тази тема в момента (1 гости, 0 анонимни потребители)
Потребители, преглеждащи темата в момента:

Topic Options Reply to this topicStart new topicStart Poll

 


Copyright © 2003-2019 | BG Development | All Rights Reserved
RSS 2.0