I'm working with symfony2 and I need to send data from javascript function to the controller.
This works when there is no data to send in the url
document.location.href="{{path("modifMalade")}}"
But I don't know how to do to put parameter on it?
All you need is just to read the docs:
http://symfony.com/doc/current/reference/twig_reference.html#path
In your case, it could look like this:
document.location.href="{{ path("modifMalade", {'param1': 'value1'})}}"
Related
I am firt time trying DELETE method with fetch in javascript. I just want to read the sent data from my fetch in php. I am doing this:
... in js
fetch(this.url+"?deleteID="+ID,{method:'DELETE'})
... in php
if(($_SERVER['REQUEST_METHOD'] == 'DELETE')){
$id= $_GET["deleteID"];
}
but it is failing and I do not know why. The definition of $_GET is "An associative array of variables passed to the current script via the URL parameters" and I am passing deleteID as a param or not ? (When the method is GET, this works)
Thanks for explanation.
Im sorry, this works... I was using wrong URL
I'm working on a laravel project and am trying to pass an array from a controller to javascript. The following code is from my controller.
$dicomfilearray = $dicom->pluck('filename')->toArray();
return view('xray.view')->withDicomfilearray($dicomfilearray);
And below is the Javascript in that's in the blade file I'm trying to pass it to.
var dicomarray = '{{ json_encode($dicomfilearray) }}';
console.log(dicomarray);
And the following is a log result from the Javascript.
["storage/uploads/storeid1/27/10/dicom/c4p4Oco3rU.dcm","storage/uploads/storeid1/27/10/dicom/RNil0NPPzQ.dcm"]
I would like to get a list from this array. Any advice or guidance on this would be greatly appreciated, Thanks.
You can make ajax call in frotend, and backend do like this
$dicomfilearray = json_encode($dicom->pluck('filename'))->toArray());
return view('xray.view')->withDicomfilearray($dicomfilearray);
when you working in javascript and need data in javascript then why you need view part. Actually, I just read your comment.
If in Ajax
so I suggest send array with json_encode and populate that data into view with javascript.
simply right below in controller
response()->json(['status'=>200,'data'=>$dicomfilearray])
Update
So ,you not sending ajax request so, simply. do like below.
controller:-
$data = json_encode($dicomfilearray);
return view('your-view',compact('data'));
javascript
var dicomarray = '{{ $data }}';
You can do something like this and this even works if you want to pass the variable to external javascript file. All you have to do is to call the init function with passed parameters.
<script>
$(function () {
init({{ json_encode($dicomfilearray) }} });
function init(dicomfilearray){
//use your variable here
}
</script>
i have a page 'index.html' with a jquery load method like this:
$(document).ready(function(){
var user = 10;
$('#content").load('profile.html', { id: user });
});
My issue is how to get this variable id in the page 'profile.html'.
Thank you.
By supplying data to the load() call, you're sending that data via a POST request to the server.
As such, on the server side if you want to use that variable, you need to access the variable id from the POST data. Without knowing what language you're using for your server side, I can't help you further.
I am calling another application context from window.showModalDialog but confused with following work. Same code to pass parameter within showModalDialg.
var myArguments = new Object();
myArguments.param1 = "Hello World :)";
window.showModalDialog("java2sTarget.html", myArguments, '');
and i can read these myArguments(parameters) in generated HTML using following code:
<script>
document.write(window.dialogArguments.param1);//Hello World :)
</script>
I can't use query string & i am sending myArguments(parameter) because i want to hide parameter from Application user.
Now i am calling servlet from showModalDialog(..)
onclick="window.showModelDialog('http://localhost:7778/app/servlet/test',myArguments,'');"
onclick="window.showModelDialog('http://localhost:7778/app/servlet/test',myArguments,'');"
But as per my knowledge
Servlet --> Servlet container --> HTML+JS+CSS
so JS will be available at last phase, but i want to use in first phase(Servlet).
Now, i need to make some Decision in servelt code based on myArguments(parameter).
is there any way to read these myArguments(parameters) in servlet code?
Pass it as a request parameter in the query string.
var queryString = "param1=" + encodeURIComponent("Hello World :)");
onclick="window.showModelDialog('http://localhost:7778/app/servlet/test?' + queryString, myArguments, '');"
No, there's no other alternative. The request URL is not visible in the modal dialog anyway.
As main objective is to hide query string from User to avoid misuse of those parameters.
I tried following work around.
Developers send hidden parameters to get relative information form source(e.g.:DataBase). And we also know that we can send hidden information in Window.showModalDialog using dialogArguments
Work Around:
(i) I got relative information from server one-step before calling Window.showModalDialog using jQuery.getJSON()
(ii) i used google-gson API at servlet side to convert JavaBeans into Json strings.Solution 1 Solution 2
(iii) Convert JSON into javascript object using jQuery.parseJSON
var args = jQuery.parseJSON(json);
window.showModalDialog("pages/"+args.pageName, args, '');
i used args.pageName to make things dynamic
Please suggest improvements in this work-around. Thanks
This is my first attempt at ajax, and I've written a submit handler that parses a form and sends the data via POST to the server as a JSON string. Here is a simplified example of what my javascript looks like
formData = JSON.stringify({'testA':{'testa':'some data'},'testB':{'test2':'more data'}});
The JSON string looks like this
{"testA":{"test1":"some data"},"testB":{"test2":"more data"}}
and I send it via post here
$.post("/some/form/page/",formData,updateForm,'json');
On the server side is where the problem rears its ugly head, this is what my query dictionary looks like when I print if from the Django view
<QueryDict: {u'{"testA":{"test1":"some data"},"testB":{"test2":"more data"}}': [u'']}>
The JSON string is the key of the query dictionary. I am not very familiar with Javascript or JSON so don't be afraid of hurting my pride by pointing out an obvious newbie mistake, because I am and I know it. ;)
Thanks,
You're sending the string as a parameter to $.post. Instead of calling "JSON.stringify()" yourself, just pass in your raw JavaScript object as the second parameter to $.post().
$.post("/some/form/page/", {'testA':{'testa':'some data'},'testB':{'test2':'more data'}}, updateForm, 'json');