简体   繁体   中英

How to pass url parameters in jQuery

I am implementing the jQuery Easy UI plugin for checkbox tree. I am having a problem while loading the node data from my action class. The url property does not seem to accept the parameters -

If i give url: '/webapp/fetchData' i'm able to get the data. But if I give

url: '/webapp/fetchData?nodeId='+nodeId my action class is not able to get the nodeId parameter. Any solution?

Edit Code ported from comment:

onExpand: function(node) {
  alert("inside expand"); 
  var nodeId = node.id; 
  url: '/webapp/fetchdata?nodeId='+nodeId ; 
}

Here is what works for me:

Solution 1: from static HTML javascript: on the caller side:

function onBeforeLoad (node, data)
{
    data.Name=name;
}

HTML on the caller side:

<ul id="ScriptTree1" class="easyui-tree" lines="true" data-options="onBeforeLoad:onBeforeLoad, lines:true, processData:false" url="someural.php"/>

Solution 2: from dynamic code: HTML

<ul id="ScriptTree2" class="easyui-tree" animate="true"></ul>

Javascript function triggered on any specific event:

function filltree ()
{
$('#ScriptTree2').tree
({  
        dataType:'json',
        method:'POST', 
        lines: true, 
        processData:false, 
        onBeforeLoad: function (node,param) { param.Name=name; return true;},
        onLoadError: function (dom) 
            { 
                if (!String.prototype.trim) 
                {
                    String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g, '');};
                }

                var sResponse = new String (arguments[0].responseText);

                alert ('Compl: ' + arguments[1] + ' ' + arguments[2].description + ' ' + arguments[2].name + '\r\nAnswer:\r\n' + sResponse.trim() + '\r\nQuery: \r\n' + decodeURIComponent(arguments.caller.caller.caller[0].data)); 

                return true;
            },
        url:'someurl.php'
    });
}

and callee script: someurl.php

<?
if ($_POST['Name']    != '') {$Name=$_POST['Name'];}      else {$Name='';};

if ($_POST) 
{
  $kv = array();
  foreach ($_POST as $key => $value) 
  {
    $kv[] = "$key=$value";
  }
  $query_string = join(" | ", $kv);
}

echo '[{"id":100,"text":"params","state":"open","children":[{"id":104,"text":"query_string: '.$query_string.'","state":"open"}]}]';
?>

Try this:

Using POST

function DoAction( id, name )
{
$.ajax({
     type: "POST",
     url: "someurl.php",
     data: "id=" + id + "&name=" + name,
     success: function(msg){
                 alert( "Data Saved: " + msg );
              }
});
}

Using GET

function DoAction( id, name )
{
 $.ajax({
      type: "GET",
      url: "someurl.php",
      data: "id=" + id + "&name=" + name,
      success: function(msg){
                 alert( "Data Saved: " + msg );
               }
 });
}

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