Zendesk API invalid response | Community
Skip to main content

Zendesk API invalid response

  • December 18, 2021
  • 2 replies
  • 0 views

wellinston

I use standard example from Zendesk API documentation:

$(document).ready(function () {
        var subject = "Test ticket #ABC";
        var body = "This is test ticket #ABC";
        $.ajax({
            url: 'https://tmstest.zendesk.com/api/v2/tickets.json',
            contentType: 'application/json',
            type: 'POST',
            beforeSend: function (xhr) {
                var auth = "Basic " + $.base64.encode("email@gmail.com/token:XXXX");
                xhr.setRequestHeader("Authorization", auth);
            },
            data: JSON.stringify({ "ticket": { "subject": subject, "comment": { "body": body } } }),
            error: function (jqXHR, textStatus, errorThrown) {
                if (jqXHR.status == 500) {
                    alert('Internal error: ' + jqXHR.responseText);
                } else {
                    alert('Unexpected error.');
                }
            }
        })
            .done(function (data) {
                console.log(data.ticket);
            })
            ;
    });

it added a ticket, but I error part is called. textStatus has "error" value, jqXHR.status is 0, errorThrown is empty string. What is wrong?

 

 

 

2 replies

CJ99
  • December 21, 2021

I think you just need quotes around your subject line, and body content. so this instead: 

data: JSON.stringify({ "ticket": { "subject": "subject", "comment": { "body": "body" } } }),




Eric27
  • December 22, 2021
It looks like you're using subject and body as variables so it would be `${subject}` and `${body}`. 

Hope this helps!