I have a problem when I try to refresh my page with a get parameter:
I initialize a hyper link with jQuery (I use coffeescript syntax)
id= $(this).data "id2"
url = window.location.pathname+'?Shop_id='+id
add2 = '<form><button>Valider</button></form>'
My controller :
[HttpGet]
public ActionResult EditProduct(string Shop_id){ ... }
each time, the url generated in href attribute is ok, the redirection is good when I try second and third time, but after I don't know why, the url doesn't contain the get parameter like:
/Products/EditProduct?
Instead of
/Products/EditProduct?Shop_id=0844839
Thank you.
Your first line id= $(this).data "id2" is syntaxtically wrong. Lets assume if you correct it the id variable gets assigned to "id2"
Next,
var url = window.location.pathname+'?Shop_id='+id
will set url to /Products/EditProduct?Shop_id=id2 this should very well hit your action EditProduct. To cross check this you could also put a console.log(variableName) after each line to check what value is being set in your add2 variable.
Mostly likely that your first line change will help you.
Related
I have a JavaScript function :
function post(aAction) {
var form = document.createElement("form");
form.action=aAction;
document.body.appendChild(form);
form.submit();
alert(form.action);
}
( I'm an experienced developer but not much experience with JScript - lifted that post() function from some website. )
I am passing this string into the function: A URL with query information:
http://testServer:3072/aQuerySite.dll/GetErrors?server=server1:5678
This URL returns a page listing error messages from the specified server: "server1:5678" - an argument passed to a server side query as
server=server1:5678
in the URL.
If I paste that URL directly into a browser and post it, the correct page with appropriate data is returned, and the browser address shows the complete URL as it was sent.
But when I pass the URL into my function, I get back a correctly formatted page but no records, and the browser address shows the URL truncated after the ? token : http://testServer:3072/aQuerySite.dll/GetErrors? The page returns showing no records because the query parameters after ? never got to the server for evaluation - query runs looking for nothing.
alert(form.action) in the function, which I added for debugging, shows the correct URL with query arguments, as does my debugger (WebStorm) and as mentioned, if I hit the URL directly from the browser, I get the correct result. I can only conclude that my URL is getting truncated in the form.submit() call.
This happens in IE, FireFox and Chrome. I also tried using ? for "?" - same result.
Why is this happening? How can I fix it?
It seems like you're trying to use a newly created form to redirect a user through its submission.
That is not necessary, as you can simply redirect the user using the following:
window.location = 'your_url';
In your case, you say that the querystring is being replaced with a simple ?.
That's because you created a form, and this form uses GET to post its data.
So if the form action is https://www.stackoverflow.com, the query string will be added with a interrogation following by the key/value pairs.
Let's suppose you have a form with two inputs named a and b, when you submit them, the query string would look like this:
https://www.stackoverflow.com?a=zzz&b=zzz
If you simply put this url in your form action, it will replace the query string with its own data when you submit it. Since your form has no named inputs, the query string will be empty, that's why you have an empty ? after the url.
My problem that I am trying to resolve is that I made an Ajax-autocorrect input feature and "onclick" of a button I get the value inside that input and attempt to make the page redirect to a Django URL.
Here is the URL inside my url.py file:
url(r'^groups/add-member/(?:/(?P<pk_group>[-\w]+))?/(?:/(?P<pk_member>[-\w]+))?/$', views.add_member, name='add-member')
inside the HTML I have this script:
<script>
function add_member(id){
var input=document.getElementById("myText").value
console.log(id) //Correctly gets ID #
console.log(input) //Correctly gets the name (string) of member instance
url = "{% url 'add-member' 0 zzzz %}".replace('0', id).replace('zzzz',input);
}
</script>
And the resulting error that I get is:
Reverse for 'add-member' with arguments '(0, '')' not found. 1 pattern(s) tried:
['flexfeed/groups/add-member/(?:/(?P<pk_group>[-\\w]+))?/(?:/(?P<pk_member>[-\\w]+))?/$']
I'm having trouble understanding my error. If anyone could suggest how to fix this issue or possibly suggest a difdferent approach to this particular problem I'd really appreciate it!
The end goal is that I want my page to redirect to my update view at the correct link!
Ok, so there is a problem with the order of what's happening. When Django renders your template it processes the tags. This happens before any javascript is called.
So you are trying to call the url with 0 and zzzz as the arguments. Django tries to fetch this and because the second argument is an empty variable here it doesn't match anything.
I would fix this by calling the url with two variables that are valid, then changing them with javascript.
url = "{% url 'add-member' 0 'change-team' %}";
url = url.replace(0, id).replace('change-team', input);
You might also be able to just put quotation marks around the zzzz.
there is a div that i want to load a url in it with a fix url. i define a variable to add my url next to that page name. but the variable is not replace . and the url broke.
how can i send a url as a variable to another url
<script>
$(document).ready(function(){
var url = 'reservation/oneway/list.bc?bcpage=12&page=1&cat=130';
$('.load-face').load('/one-wayajax.bc?url='+url);
});
</script>
the result is :
one-wayajax.bc?url=reservation/oneway/list.bc?bcpage=12&page=1&cat=130
and removed the other objects because there is two ? repeat behind together.
i guess i have to use the second part as a variable
the true result should be this : ? &
one-wayajax.bc?url=reservation/oneway/list.bc&bcpage=12&page=1&cat=130
I am trying to refresh the page while pass an array from an onclick button.
Since I am using yii, posting isn't an option, and setting a session variable wasn't working. Any ideas would help. Thank you
<a style="width:100%;" onclick="my_picks_reset()" id="my_picks_reset">Reset</a>
<script>
$(function() {
/*set var picks = array of TBA and reset th my_picks div*/
$("#my_picks_reset").click(function() {
var my_picks = ['TBA','TBA','TBA','TBA','TBA','TBA','TBA','TBA'];
var url = document.URL;
$(location).attr('href',url,'my_picks',my_picks);
})
})
</script>
It seems from your comments that you're expecting POST request. Changing location of the page will give you GET request. So you have two options here:
1) Continue using location and read the the values from $_GET variable.
If you decide to use this option your need loop through my_picks array and construct the query string that would look like that:
?my_picks[]=arrayValue1&my_picks[]=arrayValue2... and do location.assign(currentLocation + composedQueryString)
2) The second better solution is to use $.ajax() to send values with post method.
I am working with javascript and here is what I am trying to do:
1. Send a get request which looks like "http://localhost:8080/myapp/verify.htm?verifyId=Agkvhs"
My requests reaches my verify.jsp in which I have javascript which does some special things and then should redirect to another url WITH the verifyId .
My code looks like this:
function saveAndRedirect() {
var verifyId = "<%=request.getParameter("verifyId")%>";
var redirectUrl = "registrationVerify?verifyId=" + verifyId;
window.alert("Got value " + redirectUrl);
window.location = redirectUrl;
}
However this does not work. I have an alert which shows me the correct URL with the appended parameter as I expect.
In my web.xml file I have a servlet mapping with the following:
<servlet-mapping>
<servlet-name>RegistrationVerificationServlet</servlet-name>
<url-pattern>/registrationVerify*</url-pattern>
</servlet-mapping>
This mapping was workign before I appended the verifyId to the URL, I could see my request beign redicted to the servlet, since I appended this param it is not working. Any ideas much appreciated!
If this is not the ideal way to do this, please let me know any alternative.
Thanks
Your url-pattern is suspicious.
Use either
/registrationVerify
to cover the exact path or
/registrationVerify/*
to cover the part of pathinfo. Note that you don't explicitly need the last one to be able to accept request parameters, if you'd thought that.
This is a bit of a long-shot, but does this line change help?
window.location=window.location.href+redirectURL;