0

We are trying to build an embedded widget witch will be called from another domains.

My widget works fine in my local as a webpage. The problem comes when i try to call it from another page. Let's call that another site as A.

When A add my script tag, i want to make a call to my widget and embed it into A.

This is how to make call to my widget:

var widget = {
    initialize : function(containerId)
    {
        (function($) {
        var url = 'http://localhost:8002/widget.html';

        $.ajax({
           type: 'GET',
            url: url,
            async: false,
            jsonpCallback: 'jsonCallback',
            contentType: "application/json",
            dataType: 'jsonp',
            success: function(html) {
               console.log(html);
               $("#" + containerId).html(html);
            },
            error: function(e) {
               console.log(e.message);
            }
        });

        })(jQuery);
    }
}

The problem is, after reading dozens of tutorials i still could not understand how callback functions should work. I don't know if i can achieve this with a little touch or i am on the wrong way.

is it possible to return whole html page with call? I will be grateful to all advices.

erkan demir
  • 1,386
  • 4
  • 20
  • 38
  • What error do you get? In your code, the AJAX call expects JSON, but you are returning HTML, right? – Alvaro Montoro Jan 07 '15 at 17:51
  • I am getting Uncaught SyntaxError: Unexpected token < exception. That < character is first character of my widget. I am aware that call expect Json but i am trying to return html but i don't know the correct approach. @AlvaroMontoro – erkan demir Jan 07 '15 at 17:54
  • 1
    You are answering your own question. In your AJAX call, remove the references to JSON, set the `dataType` to `html` instead, and see the results – Alvaro Montoro Jan 07 '15 at 17:55
  • When i change dataType to html i am getting cross-domain exception. That's why i started to struggle with this method but i could not figure out how to complete it. @AlvaroMontoro – erkan demir Jan 07 '15 at 17:58
  • Option 1) Return JSON (e.g.: `{ "data":"...your_code..." }`; Option 2) Follow the instructions on here: http://stackoverflow.com/questions/20037491/cross-domain-ajax-request-return-html-not-jsonp – Alvaro Montoro Jan 07 '15 at 18:06
  • I'm trying to do Option 1. If i return static html as json, it works perfectly. So, how can i return my widget's html in callback function as json? @AlvaroMontoro – erkan demir Jan 07 '15 at 18:35

0 Answers0