Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Trying to stop this piece of code from running when the webpage loads.
<input type="submit" class="btn btn-info col-md-pull-4" value="Download" id="mybutton" onclick="myFunction">
<script type="text/javascript">
function myFunction() {
document.getElementById("mybutton") = #api.function();
}
</script>
Whenever I load the page, this connects to the web api and launches the api.function code. I only want this to happen when the button I have is clicked.
Am I going about this the wrong way?
From comments:
Paul: It (#api.function()) executes a workflow in an external program. Essentially just starts a process in a different application. Is there anyway to stop Razor rendering it on page load and only when I click the button?
Short answer - No. You are confusing server side and client side code. There is a distinction. The server side code is executed in the Model, in the Controller, and in the razor View. The client side code is executed using JavaScript in the context of the web browser but this code has no access to the server side code/processes. The only access it has is the same access as anything running from a browser, it uses HTTP calls to the server using URLs and passed in data.
If you want #api.function() to be called on click you have these options:
Post back - create a post back / submit and start this process from your MVC Controller. In this case remove the client script tag completely and rely on server code to start the process.
Create new public method in either your existing MVC controller OR a new Web API controller. Mark this method with HttpPost attribute and then call it using a JQuery .ajax call. This will allow the call to be submitted without the need for a form post.
I don't see why you would want to call a server method onload? can't you just load in the data that you need from start?
in any case an alternative with jquery:
$("#mybutton").on("click", function(){
// call server
})
That happens because Razor renders #api.function() and the result of the rendering is the execution of the function. Can you explain what is #api.function() and what does it do?
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am using Angular2 for the front end on a website. It will be iframed into another website. when using the HTTP post method i receive the response which is json
This is the API Post method. it will return a json result
in the front end sending of the post request i take the response and put it into json. then i set a variable in my AppService class with a set method. then in the completion of the HTTP request i open a new tab and fill it with the HTML. using the document.write() is where i get the error
This is the front end Post request
the 2 options i thought of to solve this were sending a window.postmessage() to the parent of the iFrame to try and open the new tab. Or make another iFrame inside of the current however both of these are non-optimal.
It's entirely possible that a pop-up window gets blocked by the browser. If that happens, window.open() returns null and you need to write your script to handle this situation.
Most modern browsers are pretty "clever" about blocking pop-up windows, meaning they only allow a script to open one as a direct consequence of a user interaction. Because of this, running window.open() on the completion of an AJAX request is very likely to fail. One way to solve this is to open the window immediately when the user has initiated the action, then perform the AJAX request and write to the window when it's successful. Something like this:
button.onClick = function() {
var popUp = window.open();
if (popUp) {
doAjaxRequest().then(function(data) {
popUp.document.write(data);
popUp.document.close();
});
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm kind of new to javascript but I'm currently working on my website:
When I press a button, javascript generates a random number (for example: Your Coins: 25) and then I need to connect to my 'members' table and add 25 to the 'coins' field. (I'm already connected with mysql in the php code if this matters.)
Could anyone help me?
If it's "coins" then you probably won't want it to generate client side, otherwise someone would be able to call your java script function with any number they like and add in millions of coins!
The other way is to have PHP generate the number for you.
You can use something like jQuery's $.get function to call your php script with the action of "adding a random number of coins" and the php script can return the random number to java script via JSON for it to be displayed.
First the browser sends a request to the server, which, on its turn parses it. This is the time when your HTML is generated and your PHP runs. When the HTML is generated and ready, it is being sent to the web-browser. The HTML might contain script tags which are pointing to js files via the src attribute, or script tags which contain Javascript code, but before the server sends the response, the Javascript files are not loaded, Javascript code inside the scripts will not be executed. When the response arrives to the web-browser, it parses the HTML, loads the external js, css files and pictures and executes the Javascript code.
So, when your Javascript generates the value, it is running on the user's web-browser, remote from the server. Therefore, from this point the Javascript code should send an AJAX request to the server. This will post a request to the server, which, on its turn will receive and parse it. You can post parameters when you send an AJAX request. jQuery has an easy-to-use variation. Your server will receive the request with your parameters and you will be able to read the parameters via $_GET or $_POST, which are associative arrays containing parameters with their names used as indexes. You can use those to write your query.
All this is well-documented, if you watch a few tutorials, you should be able to solve the problem. On the other hand the commenters and the other answerer are right when they tell you that you should never trust the browser to generate sensitive data, as hackers could easily see what requests are being sent from the web-browser and would send similar posts where they would be "lucky".
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a JS script on a page that makes a call to Y server on page load and it displays some data. If you look on page source you can see the script making the call to Y server.
What I need to do instead is make the api request to Y server from MY server and render the page to client completed without the JS scripts. So if you look at the page source you will not see any reference to Y server because that will all have happened on my server in the background before the page was rendered to the client.
Does anyone know how this setup can be accomplished? Looking for guidance... Links to docs? Please ask for clarification if unclear.
At a high level, you'll need to:
Implement an HTTP client for the API
Add code that calls the API (probably in a controller, but it could be a helper, model or service object)
(Optionally) parse the response (this depends on what format it's in and what format you need)
Render the parsed response in the view
2 & 4 are easy. 3 is going to be up to you, unless you can provide concrete examples. It should be easy, though. That leaves 1.
If you're using a popular API, there's a good chance that a client has already been written. If it's something in house, you can write something custom using Net::HTTP or one of the other popular HTTP client libraries. As long as you don't need to send along any cookies/headers from the browser, this should be really easy.
Here's a quick example of how this could look using RestClient and an API that returns HTML.
class SampleController < ApplicationController
def index
#mydata = RestClient.get('http://path.to/your/api?with=params')
end
end
# /app/views/index.html.erb
<h1>Here's Your Data</h1>
<%= raw #mydata %>
what you need, is to generate directly your html from your server, and render a dynamic page.
You'll have to general your html from your templates, and render it directly to the client.
For example, php, you'll have your .twig file with php variables instead of your static html.
The same goes for java, with your servlets.
The page is rendered Back side, and sent as static file to the client.
Not sure for the exact method for ROR.
I'm trying a simple example. imagine that the variable data contain the elements you want to get from your api, here is your 2 ex :
//first case staticPage1.html
[...]
var data=loadAjax(url)
window.console.log(data);
//the page served from the back is the same in the front
//second case generated.someServerSideTemplate [...]
var data={{getDataFromUrl(url)}}
//some server side templating language
generated.html [...]
var data={"message":"hello world"} // actual page rendered
i hope it's clear...
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am currently building a site which is based around an interactive button.
The Process of the site:
Someone signs up using their name, address and a unique username
(e.g. joebloggs, richardbloggs).
Once signed up they will be able to click the interactive button.
What I want the button to is:
Once the button is clicked it will automatically update a piece of text
under it with the person that has clicked its username e.g.
'joebloggs has clicked'.
If another person then clicks the button it will then say e.g.
'richardbloggs has clicked'.
The Key feature is the once it is CLICKED it updates STRAIGHT AWAY
Generally you should do it the following way:
Attach an event to the button just as #Marc B suggested
In the function for the event do a jQuery.ajax request, sending the users name (type to POST would be a good idea and the users name inside data)
On the server create a PHP file (for example setname.php) that has the URL you used for the ajax request
In the PHP file take the name from $_POST['name'] and file_put_contents it into any file (let's say lastbuttonclicker.txt)
Go back to the JS and make a window.setInterval(..., 1000); which runs another JS function
In that function from step 5, do another ajax request (via jQuery.ajax and add it via jQuery.fn.text, not jQuery.fn.html and don't use jQuery.fn.load!!! That way users could write HTML into other users interfaces doing very bad things) to another PHP file (for example getname.php)
In that PHP file just do <?php echo file_get_contents('lastbuttonclicker.txt'); ?>
But after all be aware, that users can put offensive names there. But that is a topic for itself.
EDIT: You should also ensure, that the content has a suitable length, at the server side (checking inside JavaScript has no impact to bad people writing gigabytes of text to everybodys client and your server).
EDIT 2: <facepalm> Of course you dont need step 7, just load the lastbuttonclicker.txt itself in step 6 instead of getname.php
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
everyone, I am making a website. It can generate a MS excel file using jquery and download. The question is that I hope this generated file can be sent as an attachment in an email using php, so firstly how can I pass the generated file to php?
The following simplified code illustrates my javascript code:
$(document).ready(function() {
$("#excelExport").click(function() {
var fileexample;//file declaration before generation
....//generate the file
fileexample.click();//download the file
return fileexample;
});});
I have tried some methods to solve the problem as the following, just a reference:
pass the fileexample variable using jquery ajax POST request to php server, but it cannot work. It can only work when passing normal variable like a string.
put the javascript code and php code together in a php file. But I don't how to call the javascript fileexample variable in php code.
put the javascript code and php code together in a php file and use php to generate a MS excel file. But how to call the javascript normal variable like document.getElementById("XXX") in php code.
Hope someone can help me, thanks a lot in advance.
Judging from the title a library that does it, found from google, could be
http://excelbuilderjs.com/
As it is nicely explained on the first page the main reason to do that would be to avoid contacting the server. Otherwise there is no good reason to create an excel on the client and then send it to your server.
Regarding your methods,
1.You can post xml data created at the client from js to the server jQuery ajax post to web service
2.It is not possible to call js code in php. Javascript is executed at client side, php at server side. You can merely prepare the js that will be executed at client side, within your php code at server side.
3.This is probably towards the right direction. Prepare the xls file with php at server side and providing it as a result to php call by having visited a page or a js call via ajax. Then you could allow the download of your file from the resulting php page, or via js as a response to the ajax call.