简体   繁体   中英

Javascript Convert JSONP data from string to Json object

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

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

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM