Please help me with this ridiculous problem.
I am passing a URL from my js form by using window.open() function that will hit a controller method and also some path variable is included..
that is,
myUrl = "controller/"+pathVar1+"/"+pathVar2+"/controllerMethod?AUTH_TOKEN=" + getAuthTokenId() +"&";
window.open(myUrl,true);
getAuthTokenId() is written in my "global.js" file And in the controller I have written the method as
#Controller
#RequestMapping("/controller")
public class ControllerName{
#RequestMapping(value = "/{pathVar1}/{pathVar2}/controllerMethod", method = RequestMethod.GET)
public #ResponseBody void ControllerMethodDefinition(HttpServletRequest request, HttpServletResponse response, #PathVariable("pathVar1") String pathVar1,#PathVariable("pathVar2") String pathVar2){
/***/
}
}
Now My problem is when first time the js is executing my call from js is not hitting the controller but next time onwards the controller is hit every time.
For this problem, the best way to investigate is that:
1) Put alert('Before open ' + myUrl); and alert('After open ' + myUrl); before and after your window.open() command, to make sure it was executed.
2) System.out.println("Controller hit. Pathvar 1 = " + pathVar1 + "; Pathvar 2 = " + pathVar2); in your controller to make sure that it is really hit.
My guess is that somehow at first time your URL is not properly initialized, so that the command won't run. But whatever the reason, it should be clear after you perform 2 above tests.
Related
I have created one bootstrap modal on index.jsp. When I click on save button I have navigated my controller from .jsp file to .js file where i have written requestToChangePassword() in .js file. Now my question is, from that function i want to make ajax call request so that i can enter into one of the .java file and proceed further.
I have written ajax call like following code is in .js file:
ajaxCallFunctionget("../view/passwordChangeRequest",data,handlePasswordChangeHandler,"text", true);
In java file I have written
#Controller
public class UserGridAjaxController {
static Logger log = Logger.getLogger( UserGridAjaxController.class.getName() );
#RequestMapping(value = "/passwordChangeRequest", method = RequestMethod.GET)
#ResponseBody
public String passwordChangeRequest( /*#RequestParam("userNameForEdit")String userNameForEdit,*/HttpServletResponse response )
{
log.debug( "In passwordChangeRequest() method of class " + getClass().getName() );
System.out.println( "In passwordChangeRequest() method of class !!!" );
String result = "";
//result = delegate.deleteUser(userNameForEdit);
return result;
}
}
When I hit ajax request getting following exception in browser.
VM230 jquery.js:4 GET
http://localhost:8080/view/passwordChangeRequest?fromDate=05102019&_=1557482141385 404 (Not Found)
Hi I have created small project and shared proect on GitHub Please check and let me know where I am doing wrong.
I'm trying to use a javascript loop to update data from my c# code. But it only execute the c# method once.
I made a test class :
public class testClass
{
public static int result;
public static int nb1;
public static int add()
{
result = result + 1;
return result;
}
}
And in my javascript I made this :
<script>
setInterval(Testing,3000);
nb1 = 0;
function Testing() {
nb1 = nb1 + 1; //this is to test that the function really restart after 3 seconds
document.getElementById('label').innerHTML = nb1 + " | " + #testClass.add(); ; // calling my c# method and print into a html label
}
And it display this on the page :
this is what is displayed on my html page
As you can see the number that show if the javascript function is restarting after 3 seconds works, but the c# method is not updating.
Do you have any solutions ?
It seems like you don't really have an understanding how HTML, JS and C# work in unison.
When a request is made to your server, It will perform calculations to return a document. This document is presented in HTML, with added logic in the form of JS to compliment it.
When using Razor, you can effectively use C# to help building the document, but not at runtime. If you want your front end document to be able to interact with the backend C# code, you are bound to using sockets(for example SignalR), or AJAX(which would be the most prominent way of asynchronous communication between a web page and a back end).
I will help you understand by stepping through the code with you..
Your razor page will start building. at one point it will detect the following line:
document.getElementById('label').innerHTML = nb1 + " | " + #testClass.add(); ; // calling my c# method and print into a html label
this is where it will call on the C# method, convert it to a value, and put the value in the JS code, so when the document gets sent to the client (your webbrowser) the code will look as follows:
document.getElementById('label').innerHTML = nb1 + " | " + {THE CALCULATED VALUE} ; // calling my c# method and print into a html label
I will refer you to this answer. Where it is explained how to do what you are trying to do.
So my problem in short: I have some html page with a button. When the button is clicked, I would like to move to another page. Looks pretty simple, but I can't figure it out.
So, my button handling logic looks like this:
$("#go").click(function() {
var source = $("#dropdown").text();
$.ajax({
type : "GET",
url : "mainpage.html",
data: {provider: source}
});
})
Now my controller looks like this:
#Controller
public class MainController {
#RequestMapping(value = "/hello", method = RequestMethod.GET)
public ModelAndView hello() {
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
return mav;
}
#RequestMapping(value = "/mainpage", method = RequestMethod.GET)
public String goToMainPage(#RequestParam("provider") String provider) {
System.out.println("##########################" + provider.trim());
return "empty";
}
}
So the story starts on the page associated with /hello (=index.jsp). The page is found, displayed, everything is all right. However, when I click y button, I can see the bunch of '#' signs printed out, but the page for /hello (index.jsp) is displayed again instead of the one for /mainpage (which should be empty.jsp the file is right there next to the other).
I also tried to return a ModelAndView, but that did not help. I tried to split this controller into two, but it did not help. I tried to use POST instead of GET, you know what happened...
This may be a stupid question, but I'm completely new to Spring MVC.
Ok, finally I figured it out.
I changed my jQuery function to this:
$("#go").click(function() {
var source = $("#dropdown").text();
window.location.href='/mainpage?provider='+source;
});
and controller method to GET.
This also goes through the MVC controller and correctly fetches the value for provider.
Anyways, please let me know if there's a better way to do this.
I have to send the longtime request to server in my Asp.net application, that request i called 4 services, each service take max of 1min , so i want to show prograssbar which service is completed , i searched one link Example, it is correct about my concept but that link using iframe to load another page that page write method like
protected void UpdateProgress(int PercentComplete, string Message)
{
// Write out the parent script callback.
Response.Write(String.Format(
"<script>parent.UpdateProgress({0}, '{1}');</script>",
PercentComplete, Message));
// To be sure the response isn't buffered on the server.
Response.Flush();
}
In this code to call javascript function in parent aspx page to update details , but i have in same page to handle that concept , i removed "Parent." in my code get the error object expected how to write the code in single page
With help of jQuery (http://jQuery.com) you could do the following in javascript:
function callServices() {
var serviceUrls = ["http://yourdomain/svc1", "http://yourdomain/svc2", "http://yourdomain/svc3", "http://yourdomain/svc4"];
for(i = 0; i < serviceUrls.length; i++) {
$.ajax(serviceUrls[i], function(){ updateStatus("Service " + i); });
}
}
function updateStatus(svc) {
$("#statusBar").append("<span>" + svc + " has finished.</span>");
}
Cheers!
Have Controller:
public class MyController : Controller
{
[HttpGet]
public ActionResult MyAction(int iMode, string strSearch)
{
return View();
}
}
In my view I have a div with id=center
I execute the following code in javascript
url = "/MyController/MyAction?iMode=7&strSearch=as";
$('#center').load(url);
When debugger his the breakpoint in my action on first line, iMode variable shows proper value of 7, strSearch parameter arrives as null.
Any help/advice would be most welcome.
Just use the ampersand instead of &
url = "/MyController/MyAction?iMode=7&strSearch=as";
Thanks Innatepirate for the tip. Wasted enough time trying to figure out why controller is getting null values. Replacing & with ampersand did the trick. By the way I was doing the simple age old window.location.href = link and still had the problem. Probably its the MVC parsing and routing that is messing this up.