2017-09-08 20 views
2

私はhtmlを使って応答するサーバーとコールバック関数に応答する簡単なウィジェットを作成します。ここに私のコードスニペットlaravelを使用して関数コールバックを使用する方法は?

<div id="example-widget-container"></div> 
<script type="text/javascript" src="js/library.js"></script> 

この

? ({'html': '<strong>Hello World!</strong>' }) 

からの応答pytonからこのコードやHTMLを生成し、これはコールバックを使用してサーバから来る

hello from the other site: Hello World! 

のHello Worldを対応です関数。

、ここで私のwidget.js

(function() { 

    // Localize jQuery variable 
    var jQuery; 

    /******** Load jQuery if not present *********/ 
    if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') { 
     var script_tag = document.createElement('script'); 
     script_tag.setAttribute("type","text/javascript"); 
     script_tag.setAttribute("src", 
      "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"); 
     if (script_tag.readyState) { 
      script_tag.onreadystatechange = function() { // For old versions of IE 
       if (this.readyState == 'complete' || this.readyState == 'loaded') { 
        scriptLoadHandler(); 
       } 
      }; 
     } else { 
      script_tag.onload = scriptLoadHandler; 
     } 
     // Try to find the head, otherwise default to the documentElement 
     (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag); 
    } else { 
     // The jQuery version on the window is the one we want to use 
     jQuery = window.jQuery; 
     main(); 
    } 

    /******** Called once jQuery has loaded ******/ 
    function scriptLoadHandler() { 
     // Restore $ and window.jQuery to their previous values and store the 
     // new jQuery in our local jQuery variable 
     jQuery = window.jQuery.noConflict(true); 
     // Call our main function 
     main(); 
    } 

    /******** Our main function ********/ 
    function main() { 
     jQuery(document).ready(function($) { 

      /******* Load CSS *******/ 
      var css_link = $("<link>", { 
       rel: "stylesheet", 
       type: "text/css", 
       href: "css/style.css" 
      }); 
      css_link.appendTo('head');   

      /******* Load HTML *******/ 
      var jsonp_url = "http://jsonp.local/request-json-array?=callback=?"; 
      // http://al.smeuh.org/cgi-bin/webwidget_tutorial.py?callback=? 

      $.getJSON(jsonp_url, function(data) { 
       $('#example-widget-container').html("hello from the other site: " + data); 
      }); 
     }); 
    } 

    })(); // We call our anonymous function immediately 

私の問題は、laravelを使用してJSONPによるJSONからのコールバック関数を作成する方法です。 Googleを検索しようとしていますが、これまでのところ、私のプロジェクトには十分なものが見つかりませんでした。

素晴らしい記事のためにhttp://alexmarandon.com/articles/web_widget_jquery/に感謝します。コードの大部分はそこから来ています。

UPDATED

: 私はすでに私の答えを入れて、クロスリソース起源の共有を実装するか、コールバック関数を使用するために私を有効にCORSとして知られていることにより、新しい答え

答えて

関連する問題