How do I use DownloadUrl with AJAX and Google App Engine? - javascript

The snippet of JavaScript that I am having trouble with looks like this:
var content_value = encodeURI(document.getElementById("chattext").value)
downloadUrl("/getchats", "POST", "content=" + content_value, onChatsReturned);
This code works, but it only posts the content. How would I have to change this in order to post another item, such as a description? I have all the other code ready and working, I just don't know how the parameters work for downloadUrl.

It's quite hard to say anything for certain since I can't know what downloadUrl() function actually does, but here is a solution that at might solve your problem (if it just passes the 3rd argument to the backend).
downloadUrl("/getchats", "POST", "content=" + content_value + "&description=" + desciption_value, onChatsReturned);
As the comments mention more information on the downloadUrl-function is needed to say for sure.

Related

Javascript Flask URL_for improper redirect

I know there are already several questions about the best way to do a Flask URL_for through Javascript, but I can't get it to work for me. I am new to Front End development so go easy on me here. I have tried multiple ways, and each way returns an error or a different problem.
I have a Bootstrap dropdown menu that when clicked on, should return a report to the user. I thought I had it solved earlier because I can get it to return the report if I do it this way:
$('.dropdown-item').on('click', function (event) {
branch = $(this).attr('branch');
selected_date = selected_date;
window.location.href = route_summ/branch/selected_date
The problem is then that it adds the route_summ in front of every other file, including javascript, css, etc files, so none of those can be found any longer. If instead I do:
window.location.href = "{{url_for('route_summ', branch=branch, selected_date = selected_date)}}"
As I have seen in other posts people suggest, it doesn't look at any of the variables and instead just tries going to route_summ//.
Some posts talk about using an $.ajax call, so I have done it this way:
$.ajax({
type: 'GET',
url: "/route_summ/" + $(this).attr('branch') + '/' + selected_date,
data: {
branch: $(this).attr('branch')
},
method: 'GET',
success: function(response) {
console.log('Success');
window.location.href = "{{url_for('route_summ', branch = branch, selected_date = selected_date)}}"
}
});
But that doesn't work either. It establishes a call and returns "Success", but it doesn't redirect to the site. If I were to just go to the site, it works, but I can't get it to link to it without screwing up the other dependent links on it. Can anyone please help me? I'm at my wit's end here.
Okay I figured it out. My first solution would've worked, but I needed to put a "url_for" in my tags. I'm going to leave this up in case any other novices run into this problem.

appending something to a bookmarklet-generated url

I'm using a bookmarklet to post URLs to my known installation.
javascript:(function(){window.open('http://xyz/known/share?share_url='+encodeURIComponent(location.href)+'&share_title\='+encodeURIComponent(document.title));})();
This returns the URL:
http://xyz/known/share?share_url=http%3A%2F%2Fwww.stichpunkt.de%2Fbookmarklets%2F&share_title=Bookmarklets
I want to have
&share_type=bookmark
appended to the url, yet since I don't understand JavaScript, fail to.
You can try this:
javascript:(function(){window.open('http://xyz/known/share?share_url='+encodeURIComponent(location.href)+'&share_title\='+encodeURIComponent(document.title) + '&share_type=bookmark');})();

multiple ajax calls is causing mixed datasets

I've been developing a phonegap application which uses jQuery Mobile for it's main structure.
When the site/app runs, it executes a number of ajax calls to get the latest data to the app. For example, a list of items is gathered for the homepage, whilst other lists of other items are gathered for other pages.
What I am finding is that every now and then there is a mixup in data.
As a random (but applicable) example:
Query 1 - get names and photos of people
Query 2 - get names and photos of cities/locations
In each of the ajax calls, instead of using (data, status) I have renamed the data object to a unique identifier hoping this would resolve the issue.
Then, on my $.each function I have ensured that the iterator has a different name too, so instead of (i, item) it might be (i, personitem) and (i, cityitem)
The Issue
Despite my best attempts to get this data to not have any possibility of crossover, I'm finding that (to keep with the current example) - photos of people will show up on the cities page, and photos of cities will show up on the users page.
This is also an intermittent issue. Sometimes it won't happen at all, other times it will happen a lot or only a little bit.
I hope I have explained myself clearly. Thank you in advanced to anyone willing to help! I'm all out of ideas :-(
==================
UPDATE
My main question is if anyone knows what might cause data-mixups in such queries.
My queries all look like this:
$.ajax({
url: 'get_cities.php?country='+country,
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(citydata, status){
if(citydata.length == 0){
$('#somediv').append('no data to show');
}
$.each(citydata, function(i,cityitem){
var content = '<img src="'+cityitem.image+'" />'
+'<p>'+cityitem.name+'</p>';
$('#somediv').append(content);
}
});
At this point I believe #mason81 was correct with his suggestion.
I removed all of the:
jsonp: 'jsoncallback',
in my code, and then updated my PHP files to use
echo $_GET['callback'] . '(' . json_encode($records) . ');';
instead of
echo $_GET['jsoncallback'] . '(' . json_encode($records) . ');';
I originally also placed &callback=? into my URL's but from reading the jQuery Ajax documentation it should automatically assign a random unique callback to each call as long as you are using dataType: jsonp without manually specifying a callback parameter.
If this fails I'll still consider this the correct answer, I'll probably just have to go through and either assign unique callback parameters to each request or ammend my URL's with &callback=? as mentioned above.
Thanks everyone for their input!

connecting javascript to a Web API

I am new to the web development world and I would like to be able to connect an HTML page to a web api through . and I was really not successful in this.
I followed this tutorial to be able to make this connection : http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
All I need is to send some inputs from an HTML page to a web api that takes these parameters and returns an object
I am using this code
$.getJSON("api/GeneratorController/setparameters/"+firstparameter+"/"+secondparameter+"/"+thirdparameter+"/"+fourthparameter+"/"+fifthparameter+"/"+sixthparameter,
function (data) {
alert(data); //never comes here
}).fail(function (jqXHR, textStatus, err) {
alert("All checks are correct, image was not generated. jqXHR = " + jqXHR.valueOf() + " textStatus=" + textStatus + " Error" + err);
});
it always goes into the fail portion , I attached the alert message that comes out of it
Any Reason why it is doing this ?
#smartmeta (I changed the typo , thanks) I followed your advice and here is the output of the alert (as expected , values that I have inserted are displayed):
Your url needs to start with your domain, not 'api/generatorcontroller/...'. If you are developing locally, something like http://localhost:[port]/api/generatorController/....
Also, webApi maps to url verbs, (get, post, put, delete..), not functions like setparameters, unless you have a [name=setparameters] above your get() function.
Also, I am pretty sure you don't have a route setup to handle the url with all those parameters. What you want to look at, as it seems your using jQuery, is jQuery.get documentation. The second example near the bottom shows where to place parameters. WebAPI will check for them in the body if they are not in the query string. so it would end up looking like:
$.getJSON("http://"+window.location.host+"/api/GeneratorController/setparameters", {parameter1: parameter1, parameter2:parameter2 ...});
Well, the first thing to check is to make sure that your server-side function is returning the values you expect. You can do this with Chrome's developer tools or with the Firebug Firefox extension, and I think IE10 has something equivalent, too. Go to the "net" tab, find the request corresponding to your API call, and take a look at what the server responded with.
Please add the line
alert("api/GeneratorController/setparameters/"+firstparemeter+"/"+secondparameter+"/"+thirdparameter+"/"+fourthparameter+"/"+fifthparameter+"/"+sixthparameter)
Then call your script and take the output of the alert into a browser. Then check if your application Handels that route.
By the way I think you have a typo. I guess it should be firstparameter.
I assume you would like to do
"api/GeneratorController?foo=Bar
But when you are new to this, I would suggest that you first try the example like it is. And After that you can start changing setails.
So I found what was the problem with my code
Two things :
1- I shouldn't use the word "Controller" when I call my API ,it should be api/Generator/...
2- the function name needs to start with "get" and not "set" since it "gets" the return value from the api
Thanks everyone!

Javascript redirect

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;

Categories