jqueryajaxsafe-browsing

ajax response isn't pre-processing as expected


I'm trying to parse the response data from querying google's safe browsing api and the response seems to be a javascript prototype object, in which i'm having no luck trying to convert or parse to JSON. My ajax call is the following:

$( document ).ready(function() {
console.log( "ready!" );

//first fake links and event listeners
AddFakeLinks(faulty_links, curr_list);

//url checker action
$("#my_form").submit(function(event){
    event.preventDefault(); //prevent default action 

    var api_url = "https://safebrowsing.googleapis.com/v4/threatMatches:find?"
    var key = 'AIz________________________ns'
    api_url += "key="
    api_url += key

    var payload = 
    {
        "client":{
            "clientId": "_________________.googleusercontent.com",
            "clientVersion": "1.0.0",
        },
        "threatInfo": {
            "threatTypes":      ["MALWARE", "SOCIAL_ENGINEERING"],
            "platformTypes":    ["WINDOWS"],
            "threatEntryTypes": ["URL"],
            "threatEntries": [
              {"url": "http://www.pitt.edu/"},
              {"url": "http://www.exchange.pitt.edu/"}
            ]
          }
    };

    $.ajax({
        type: "POST",
        url: api_url,
        contentType: "application/json",
        dataType: "json",
        data: JSON.stringify(payload),
        xhr: function(){
            //upload Progress
            var xhr = $.ajaxSettings.xhr();
            if (xhr.upload) {
                xhr.upload.addEventListener('progress', function(event) {
                    var percent = 0;
                    var position = event.loaded || event.position;
                    var total = event.total;
                    if (event.lengthComputable) {
                        percent = Math.ceil(position / total * 100);
                    }
                    //update progressbar
                    $("#upload-progress .progress-bar").css("width", + percent +"%");
                }, true);
            }
            return xhr;
        },
        success:function (data) {
            console.log(data);
        },
        error:function(jqXhr, textStatus, errorThrown) {
             console.error(textStatus, errorThrown) 
        }
    }).done(function(response){
        $("#server-results").html(response);
        console.log(response);
    });

The console has the following output: enter image description here

The network console is shown with response of 200 ok:

enter image description here


Solution

  • I'm not familiar with this API at all. But after looking at the documentation, the issue appears to be that there is no match for your request.

    https://developers.google.com/safe-browsing/v4/lookup-api

    Response body

    The response body includes the match information (the list names and the URLs found on those lists, the metadata, if available, and the cache durations). For more details, see the threatMatches.find response body and the explanations that follow the code example.

    Note: If there are no matches (that is, if none of the URLs specified in the request are found on any of the lists specified in a request), the HTTP POST response simply returns an empty object in the response body.