Seth Spivey
Seth Spivey

Reputation: 367

Ajax response returning empty after success

I pushed this code to my production site and for some reason the response on this particular call is not returning anything now. It is working on my staging site as well as through a postman request. Could anyone help me understand why the response would suddenly always be returning any empty value? Again, this is currently working on my staging site as well as through a postman request so I know it's not URL encoding

        const Url = 'https://cors-anywhere.herokuapp.com/https://scriptco.jitbit.com/helpdesk/api/ticket';

        var pharmacyName = jQuery("div.pharmacy_name").text();
        var pharmacyPhone = jQuery("div.pharmacy_phone").text();
        var pharmacyAddress = jQuery("div.pharmacy_street").text();
        var pharmacyCity = jQuery("div.pharmacy_city").text();
        var pharmacyState = jQuery("div.pharmacy_state").text();
        var pharmacyZipcode = jQuery("div.pharmacy_zip").text();

        var drug_list = jQuery('div.drug_list ul li').map(function() {
            return jQuery(this).text();
        }).get().join(',');

        const ticketUrl = "?categoryID=406340&body=" + "<h5 style='text-decoration:underline;'>Pharmacy Name:</h5>" + pharmacyName + "<h5 style='text-decoration:underline;'>Pharmacy Phone:</h5>" + pharmacyPhone + "<h5 style='text-decoration:underline;'>Pharmacy Address:</h5>" + pharmacyAddress + "<br/>" + pharmacyCity + ", " + pharmacyState + " " + pharmacyZipcode + "<h5 style='text-decoration:underline;'>Drugs/Rx's:</h5>" + drug_list + "&subject=Transfer Request&priorityId=0";

        var finalUrl = Url + ticketUrl;


        var form = new FormData();

        var ticket_settings = {
            "url": finalUrl,
            "method": "POST",
            "timeout": 0,
            "headers": {
                "Content-Type": "application/x-www-form-urlencoded",
                "Authorization": "Basic <?php echo $jitbit_encoded ?>",
            },
            "processData": false,
            "mimeType": "multipart/form-data",
            "contentType": false,
            "data": form
        }

        jQuery.ajax(ticket_settings).done(function (response) {
            console.log(response);
            fillCustomFields(response);
        });

Upvotes: 1

Views: 76

Answers (1)

Ritesh Khandekar
Ritesh Khandekar

Reputation: 4005

When you are manually creating URLs using dynamic values then don't forget to encode using encodeURI() function:

const ticketUrl = "?categoryID=406340&body=" + "<h5 style='text-decoration:underline;'>Pharmacy Name:</h5>" + pharmacyName + "<h5 style='text-decoration:underline;'>Pharmacy Phone:</h5>" + pharmacyPhone + "<h5 style='text-decoration:underline;'>Pharmacy Address:</h5>" + pharmacyAddress + "<br/>" + pharmacyCity + ", " + pharmacyState + " " + pharmacyZipcode + "<h5 style='text-decoration:underline;'>Drugs/Rx's:</h5>" + drug_list + "&subject=Transfer Request&priorityId=0";
ticketUrl = encodeURI(ticketUrl)
// ...

Upvotes: 1

Related Questions