Nevtu Nevtu - 8 months ago 38
jQuery Question

JS $.get or $.getScript pass data to file

I want to pass data to the js file. however cant get is to work.


function loadjs(js){
$.get(js,{ name: "test" });



if i do alert('test'); i get a response from the file so i know it works.
also cant use globals.

is there a simple way to send the data to the file and show it?

EDIT to clarify
(yes i can use angelar but i dont)

Onload it triggers 'pagina' with all short of variables. (its dynamic).
all the 'php/html' files are loaded.
and you get what you can see in the link. example

hoverever when i click on a row i want function pagina(); to load the content of the row so far so good. however to show the data that i want. I want to send a ID to the JS file and use a $.post (php) in the JS file to get data json. if i set a ID in $.post id=1 directly its works.

so to not let it be more confusing. i want to send a id to a js file JS $.get or $.getScript.

function pagina(menu,frame,vars,crum,js,togglezoeken,voorwaarden){
'use strict';
$('#menu').load(menu+vars,{noncache: new Date().getTime()}, function() {$('#menu').foundation();});}
if(crum==1){$('#breadcrumbs').load('/opbouw/frames/breadcrumbs.php?'+vars,{noncache: new Date().getTime()}, function() {});}
$('#frame').load(frame+vars+'&crum='+crum,{noncache: new Date().getTime()}, function() {

$.get( js,{ name: "test" });


You can't do it that way. $.get is used for AJAX/HTTP(s) requests and your file in that way won't accept the data sent to it.

To achive this you have to use a server-side endpoint on your article.js.

Lets say:

function loadjs( js ) {
    $.get(js, { name: "test" });

And you make a call of loadjs function:


On your server you have to have an server-side endpoint directing to a request: /article.js:

You can achieve this by using PHP or express for node.js:

router.get('article.js', function(req, res, next) {
    res.json( );

Or in PHP (note in Apache you have to use mod_rewrite and enable it with .htaccess or in your virtual host configuration):


if($req == 'article.js') {
    echo json_encode(array('data' => $_GET['data']));
    exit(0); // end the script here

In either way used from above you have effectivelly sent a data to your script / endpoint and you can handle it further.

Although your loadjs function just sends a request and doesn't handle a response, you can do the following with a callback function in $.get:

$.get(js, { name: "test" }, function(data) {
    alert(data); // you will alert the data from the JSON response

Sadly, you haven't described what you actually need and this is as far as I can help you with so far.