0

So i am not sure if i am doing this right.
I want to send markup over HTML (i am trying to create a widget) Here is the mocky response that i am expecting

so I create a simple jquery get like this

var jsonp_url = "http://www.mocky.io/v2/5c9e901a3000004a00ee98a1?callback=myfunction";
    $.ajax({
                    url: jsonp_url,
                    type: 'GET',
                    jsonp: "callback",
                    contentType: "application/json",
                    success: function (data) {
                     $('#example-widget-container').html(data.html)
                    },
                    error: function (data) {
                        alert('woops!'); //or whatever
                    }
                });

then created myFunction

function myfunction(data) {
        console.log(data);
}

The problem being that while, i get the response it comes as a string instead of a json or function. i am not sure how to extract the json from this (unless i do string manupulation). Any pointers would be helpful.

JSFiddle here

P.S. Per https://www.mocky.io/ ,
Jsonp Support - Add ?callback=myfunction to your mocky URL to enable jsonp.

3
  • type: 'GET', is the default, you can remove it Commented Mar 29, 2019 at 22:14
  • jsonp: "callback", is the default, you can remove it Commented Mar 29, 2019 at 22:14
  • contentType: "application/json", is just wrong, you are making a GET request, the request has no body to describe the content-type of. Since you are using JSONP, this ignored anyway. JSONP doesn't allow you to set headers explicitly. Commented Mar 29, 2019 at 22:15

1 Answer 1

1
  1. Delete function myfunction.
  2. In the URL, replace callback=myfunction with callback=?.

jQuery will generate a function (your success function) and a function name for you.

Sign up to request clarification or add additional context in comments.

2 Comments

i tried that. It still returns a string wrapped with a brackets. It is still a string though.
You can see that in the jsfiddle I had added.

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.