1

I'm trying to send some text to Github's markdown API and get back a raw HTML representation of it.

Currently I have this code:

    $.ajax({
        type: "POST",
        dataType: "jsonp",
        processData: false,
        url: "http://api.github.com/markdown/raw",
        data: {
            "text": $('#some_textarea').val()
        },
        success: function(data){
            console.log("success!");
            console.log(data);
        }, 
        error: function(jqXHR, textStatus, error){
            console.log(jqXHR, textStatus, error);
        }
    });

but I get "error" (textStatus in error callback). What am I doing wrong?

7
  • what error have you got? Commented Mar 20, 2013 at 9:45
  • The value of textStatus is "error", nothing more and nothing less. Also, the value of error is empty string. Commented Mar 20, 2013 at 9:49
  • You cannot make a JSONP POST request. JSONP is always GET, since it just appends a <script> tag to the document. jQuery is probably doing the JSONP request as asked, which is not understood by the API. Commented Mar 20, 2013 at 9:49
  • @FelixKling I get the exact same result if I change it to "json" Commented Mar 20, 2013 at 9:50
  • 2
    Well, the API does not return JSON, it returns simple text. If you set dataType: "json", then jQuery will try to parse the response as JSON, which will fail. And if the API does not support CORS (but I seem to remember it does), you cannot make an Ajax request to it from the browser anyway. Commented Mar 20, 2013 at 9:52

1 Answer 1

3

You need to post to HTTPS not HTTP, and if you're using the raw API then

  • the posted content type needs to be text/plain
  • the API returns html content, not JSON or JSONP

e.g. jsfiddle

$.ajax({
    type: "POST",
    dataType: "html",
    processData: false,
    url: "https://api.github.com/markdown/raw",
    data: "Hello world github/linguist#1 **cool**, and #1!",
    contentType: "text/plain",
    success: function(data){
        console.log("success!");
        console.log(data);
    }, 
    error: function(jqXHR, textStatus, error){
        console.log(jqXHR, textStatus, error);
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Great! Beware the API has a rate limit of 60 requests per hour. If you need more you need to register as an app and authenticate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.