Jquery Ajax: Cannot assign to a function result error - javascript

In my ajax servlet I am setting a list of objects to request attribute.
request.setAttribute("testModelList",testList);
In my script I am trying to retrieve the attribute and set in to hidden variable.
// my JS
$('#testDetails').val() = '${testModelList}';
My jsp
<INPUT type="hidden" id = "testDetails" >
But I am getting an error
Cannot assign to a function result
Can anyone help me how to get the request attribute list to the hidden variable in JSP through Ajax?

You can't assign a value to the result of a function:
$('#testDetails').val() = '${testModelList}';
But you can pass the value to the function:
$('#testDetails').val('${testModelList}');

Related

Passing multiple parameters from client HTML to server

I have a HTML form allowing users to fill out questionnaires, I would like to send the filled form as well as some variables (accessible within the HTML).
I've successfully sent filled form on it's own with...
HTML...
<button onclick="google.script.run.processForm(this.form)">Submit</button>
Javascript...
function processForm(formData) {...}
But when trying to send this.form along with extra variables / parameters, either via two parameters method...
<button onclick="google.script.run.processForm(this.form, var1)">Submit</button>
function processForm(formData, var1) {...}
or wrapped inside an array (below), both fails...
<button onclick="google.script.run.processForm([this.form, var1])">Submit</button>
function processForm(array) {...}
I've also tried declaring the array separately but same result...
HTML...
<? var arr = [this.form, var1] ?>
<button onclick="google.script.run.processForm(arr)">Submit</button>
JavaScript...
function processForm(arr) {...}
What am I doing wrong?
When the form object is sent to the Google Apps Script side using google.script.run, the form object is parsed on the internal server side and the value can be retrieved as an argument of the function at the Google Apps Script side. In this case, it seems that the 2nd argument cannot be used. I'm not sure whether this is the current specification or a bug.
If you want to include a value except for the form object, as a simple modification, how about the following sample script?
Sample script:
HTML & Javascript side:
<form>
<input type="text" name="input1">
<button onclick="sample(this.form);return false;">Submit</button>
</form>
<script>
function sample(e) {
const addValue = "sample"; // This is a value you want to add.
const input = document.createElement('input');
input.setAttribute('name', "addValue")
input.setAttribute('value', addValue);
e.appendChild(input);
google.script.run.processForm(e);
}
</script>
In this modification, a value you want to add is added to the form object. By this, the form object can be parsed by including the added value.
When this HTML is opened and put a sampe value of "test" into the input tag and the button is clicked, formData of function processForm(formData) {...} is {"addValue":"sample","input1":"test"}. So, you can retrieve your added value with formData.addValue.
Note:
As another direction, I think that the method that the form object is parsed and the value is included in the parsed value in the Javascript side can be also used. In this case, how about the following modification? In this case, in order to parse the form object, I used HtmlFormObjectParserForGoogleAppsScript_js.
HTML & Javascript side:
<script src="https://cdn.jsdelivr.net/gh/tanaikech/HtmlFormObjectParserForGoogleAppsScript_js/htmlFormObjectParserForGoogleAppsScript_js.min.js"></script>
<form>
<input type="text" name="input1">
<button onclick="sample(this);return false;">Submit</button>
</form>
<script>
async function sample(e) {
const addValue = "sample"; // This is a value you want to add.
const obj = await ParseFormObjectForGAS(e.parentNode); // Heare, this library is used.
obj.addValue = addValue;
google.script.run.processForm(obj);
}
</script>

Undefined variable when submitting an ng-change

I'm trying to store a variable from the date type input in html using angularJs. I've done this previously in this application with select tags instead of input tags and its working fine so I know all the data is correct.
My input code:
<input type="date" ng-model="x.TARGET_DATE" ng-change="updateTargetDate(x.TASK_ID)">
My function code:
$scope.updateTargetDate = function (TASK_ID) {
console.log($scope.TASK_ID);
};
In the console.log I get = undefined
I have this working in the column before this so I know its not data.
TASK_ID is the name of function's parameter. It is not a $scope variable I guess, so you should access like this:
$scope.updateTargetDate = function (TASK_ID) {
console.log(TASK_ID);
};

$(document).ready(function() stops working as soon as i assign value to variable ( var userid = ${userid}; )

I just want to be able to access variable userid in document.ready function. It's accessible in the JSP page but not in jQuery. I am able to print value of ${userid} on the same JSP page but not in jQuery. I have passed userid obj with model and view object to this JSP page. However I am not able to access that object.
$(document).ready(function(){
var examid = ${userid};
alert("Hello !");
//alert(username);
});
If the value of ${userid} is a string then you need to wrap it in quotes, otherwise you'll be getting a syntax error thrown in your JS code.
For example var examid = '${userid}';
– Rory McCrossan Apr 4 at 10:45

How to access a variable in a function from inside a jsp tag

I have the following function:
<script>
function assign()
{
var val = "";
val = document.form1.text1.value;
alert(val);
}
I want to access the variable val's value inside jsp tag so that i can pass it to the googlePlus method as a String. I tried making var val as a global variable, but it doesnt work. How can I access the variable val inside the following code?
<%
String output = "";
if ( Boolean.valueOf(request.getParameter("submitted")) == true ) {
Scraping h = new Scraping();
output = h.googlePlus();
}
%>
You can assign the value of the variable using a assignment operator to some hidden JSP field and then use it in the JS using document.getElementById(). the code would be somewhat like:
<input type="hidden" value="<%=output%>">
Or alternatively if your js is residing inside the JSP only
var s = "<%=output%>"; should work!
Cheers!
You can't access javascript variables via JSP Java Code.
Your JSP & Java codes are compiled at server side.
And your javascript runs in a browser.
So send the 'val' variable to your servlet by form submit, ajax or whatever.

Pass value from javascript in jsp to action class in struts2

how can i pass value from javascript to a java class in struts2?
This is the link similar to what I want.However it does not work for me.
I have tried something like this.
Struts tag for hidden in jsp is
function redirect(id)
{
window.alert("Hello"+id);
document.getElementById('param_ID').value=id;
document.forms["./ListAlgorithmAction"].submit();
}
In my action class I have declared getter and setter for the param_ID.But it returns null.
Kindly help.
Thank you.
You should use like this
<s:hidden id = "param_ID" name="xyz" />
Now you should have a getter and setter for xyz property in your struts action.
You should always have name attribute in tags mapped to attributes in Struts action.
Hope this works.
If the page is not submitting your hidden field, maybe the hidden field is not inside your form element. You can try pass it as a parameter to your url. Try this
function redirect(id)
{
var form = document.forms[0];
var url = './ListAlgorithmAction?param_ID=' + id;
form.action = url;
form.submit();
}
Let me know if this works.
If you want to call an action and return data you should use struts2-json plugin JQuery script for calling action
var abc = $("#abc").val();
$.getJSON('youraction.action', {
variable -name action class : abc,
value to be passed:value,
//and so on
}, function(data) {
//your processing code
}
});
return false;
});
If you want to just call action you may want to use $.ajax in place of $.getJson
If you want to use struts 2-json then you would also need to change your struts.xml i.e
<result name="success" type="json"></result>
It's worth a go
I had a similar issue and resolved it by appending the Action class from JavaScript function and appending the URL parameters as shown below:
<a href="(<%actionClassName%>.action?<%URLParms%>" />

Categories