spolu / breach_core

A Browser written in JS. Free. Modular. Hackable.
https://breach.github.io/breach_core/
MIT License
5.39k stars 411 forks source link

Ajax requests in modules? #144

Closed matthewjamesr closed 10 years ago

matthewjamesr commented 10 years ago

Hello I am making a module and want to pull data from mysql. I setup my ajax call and my php file for server side and on my server the request works, in the module on index.html it does not. Any ideas? Bellow is my clien-side:

<h3>Output: </h3>
  <div id="output">this element will be accessed by jquery and this text replaced</div>

  <script id="source" language="javascript" type="text/javascript">

  $(function () 
  {
    //-----------------------------------------------------------------------
    // 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
    //-----------------------------------------------------------------------
    $.ajax({                                      
      url: 'http://breach.matthewreichardt.com/data/request/synch.php',                  //the script to call to get data          
      data: "",                        //you can insert url argumnets here to pass to api.php
                                       //for example "id=5&parent=6"
      dataType: 'json',                //data format      
      success: function(data)          //on recieve of reply
      {
        var id = data[0];              //get id
        var vname = data[1];           //get name
        //--------------------------------------------------------------------
        // 3) Update html content
        //--------------------------------------------------------------------
        $('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname); //Set output element html
        //recommend reading up on jquery selectors they are awesome 
        // http://api.jquery.com/category/selectors/
      } 
    });
  }); 

  </script>
spolu commented 10 years ago

This is probably due to cross-origin policies. You should do the request server-side within the module and open local routes to handle that. Make sense?

matthewjamesr commented 10 years ago

Would you be able to give me a simple example geared towards what my code is trying to do? I would appreciate it.

On Tuesday, July 15, 2014, Stanislas Polu notifications@github.com wrote:

This is probably due to cross-origin policies. You should do the request server-side within the module and open local routes to handle that. Make sense?

— Reply to this email directly or view it on GitHub https://github.com/breach/breach_core/issues/144#issuecomment-49056260.

Matthew Reichardt Technology Director any.TV www.any.tv

Email: mattr@any.tv | Skype: matthewjames12 | Motto: "Set out to be what you aspire to be"

spolu commented 10 years ago

You can look at mod_strip client side: https://github.com/breach/mod_strip/blob/master/controls/strip/index.html How it uses socket.io to communicate with the nodeJS app running in a separate process as a module: https://github.com/breach/mod_strip/blob/master/lib/strip.js

matthewjamesr commented 10 years ago

Ok thanks I will take a look tonight!

spolu commented 10 years ago

perfect!

matthewjamesr commented 10 years ago

This issue has been resolved, simply create a server side php script if that is your flavor, change the headers and echo back your data request in Json. I achieved the login utilizing creation/tear down of localStorage keys.