Right now I can use this URL to request a Google Static Maps image successfully:
http://maps.google.com/staticmap?center=37.687,-122.407&zoom=8&size=450x300&maptype=terrain&key=[my key here]&sensor=false
However, the second I use JQuery or any direct javascript to set an image's src to the above url, Google passes back a Error 400:
"Your client has issued a malformed or illegal request."
I've read that this is usually from the key being incorrect, but my key is clearly being passed.
This is how I'm setting the image dynamically:
document.getElementById('my-image-id').src = "http://maps.google.com/staticmap?center=37.687,-122.407&zoom=8&size=450x300&maptype=terrain&key=[my key here]&sensor=false"
I've replaced [my key here] with my correct key, and it still doesn't work. When I request the same url through the browser, it's fine. I've confirmed that the correct referrers are getting passed as well.
Any ideas?
Does this code work for you (it works for me -- be sure to insert your key)?
<html>
<head>
<script language="javascript">
function swap() {
document.getElementById('my-image-id').src = "http://maps.google.com/staticmap?center=37.687,-122.407&zoom=8&size=450x300&maptype=terrain&key=&sensor=false"
}
</script>
</head>
<body>
<img src="http://www.google.com/intl/en_ALL/images/logo.gif" width="450" height="300" onClick="swap();" id="my-image-id" />
</body>
</html>
You have a possible typo with the g on the end here:
&key=[my key here]g
The problem is with the key. Register for a new key and append with the url. It will work fine. I even was faccing same problem. It was working with direct paste in address bar of browser. But in production it was not working. I put new key and it worked fine both in production and browser.
Related
I am building a flask website and am trying to make a search bar that is always on the top menù, I would have liked to handle it in the Python backend but I could not find a way to do that without including whithin each backed page the code to handle it (which if it is the only way is fine) so I decided to try making it work with JavaScript. This is my code (without the parts which I found unrelated to the problem):
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form>
<input type="search" id="searchbar"/>
<button id="goto">Search</button>
</form>
<script>
document.getElementById("goto").addEventListener("click", goTo);
function goTo()
{
var result = document.getElementById("searchbar").value;
window.location.href = "/users/"+result;
}
</script>
</body>
</html>
What it is supposed to do is redirect me to "http://myip/users/WhatItyped" while it just adds "/?" to the end of the URL, any idea of why this is?
Update: I found out that if I add an alert before defining result it works
Try using window.location.pathname
JavaScript Window Location states href points to the full URL, including protocol & hostname. Since you are not changing either of those, you can just change the relative path from the root using pathname.
This has also been answered before:
How can I extract and then change the url path using javascript?
function demoHTML(request, response)
{
var html = ' <html> <body> <h1>Hello World</h1> </body> </html>';
response.write( html );
//prefix header with Custom-Header. See nlobjResponse.setHeader(name, value)
response.setHeader('Custom-Header-Demo', 'Demo');
}
After deploying the Script the following error is coming...
ERROR
You are not allowed to navigate directly to this page
I had the same problem, make sure the Status is set to "Released".
As #Rockstar stated, check the Audience tab on your Script Deployment. Make sure that the applicable Roles or Employees are allowed to access the page, and make sure that the user/role that you are using have access as well. By default, no permissions are given to anyone on the Deployment.
In Google CSE, when I attempt to get Popular Queries, I'm getting this error in the FireBug Console:
NetworkError: 400 Bad Request - http://www.google.com/cse/api/xxxxxxxxx:xxxxxxx/cse/xxxxxxx/queries/js?callback=(new+PopularQueryRenderer(document.getElementById(%27queries%27))).render .... .. ..
Why is it happening? I just simply copy/paste the code from Google:
<html>
<head>
</head>
<body>
<!-- CODE COPIED FROM GOOGLE : START -->
<div id="queries"></div>
<script src="http://www.google.com/cse/query_renderer.js"></script>
<script src="http://www.google.com/cse/api/XXXX184908680XXXX:xxxxywrndxx/cse/xxxtywrnxxx/queries/js?callback=(new+PopularQueryRenderer(document.getElementById('queries'))).render"></script>
<!-- CODE COPIED FROM GOOGLE : END -->
</bodY>
</html>
I just figured this out, at least for me. The code Google gives you is wrong. They give you the URL:
http://www.google.com/cse/api/USERID:CSEID/cse/CSEID/queries/js?…
This puts the CSEID in twice. I was able to get it working by removing the first instance of the colon and the CSEID:
http://www.google.com/cse/api/USERID/cse/CSEID/queries/js
I'm not surprised they got confused: they use the term User Id to refer to the User string, but they use the term CSE Id to refer to both the portion after the colon and the combined User Id + colon + CSE Id.
I am having trouble displaying some jason from a page.
The data is there but I think it might have to do with this line:
document.write(fbResults.cats[0].title);
Here is the full html source:
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.getJSON('http://mydomain.com/api/get_cats', function(fbResults) {
document.write(fbResults.cats[0].title);
});
});
</script>
</head>
<body>
</body>
</html>
And here is the data that it's reading:
{"cats":[
{"id":"1","title":"mytitle1","colour":"#EE297C"},
{"id":"2","title":"mytitle2","colour":"#EE412F"},
{"id":"3","title":"mytitle3","colour":"#F5821F"},
{"id":"4","title":"mytitle4","colour":"#00AEEF"},
{"id":"5","title":"mytitle5","colour":"#00B495"},
{"id":"6","title":"mytitle6","colour":"#006476"}
]}
It is not displaying anything on the page.
On firebug console I get this error:
The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must to be declared in the document or in the transfer protocol.
No traces of the json data there
What I'm I doing whong?
You shouldn't document.write after the page has loaded (which is certainly the case here).
If you want to write it to the page, you'll need to create HTML and append it. Just replace the document.write:
$('body').append('<p>'+fbResults.cats[0].title+'</p>');
Update:
Your example makes a fully qualified URL call. Is that server the exact same one that you're running the page from? If it isn't the XHR will just eat the request (and sometime not tell you). If you need to go cross domain, you'll need to use JSONp. If you're attempting to run this locally while pulling data from the net, it'll break.
Try this
$.each(fbResults.cats,function(index,item){
document.write(item.title);
});
Working sample : http://jsfiddle.net/zWhEE/8/
its seems work for me please check this
http://jsfiddle.net/TxTCs/1/
i have an object element in my html body to show an Active reports which exports to a .pdf file. I need to use javascript to automatically print the pdf out to the client's default printer and then save the pdf to the server:
<script language="javascript" type="text/javascript">
// <!CDATA[
function PrintPDF() {
pdf.click();
pdf.setActive();
pdf.focus();
pdf.PrintAll();
}
// ]]>
....
<body onload="return PrintPDF();">
<form id="form1" runat="server">
<object id="pdfDoc" type="application/pdf" width="100%" height="100%" data="test.aspx?PrintReport=yes&SavePDF=yes"/>
</form>
</body>
With the data hard-code in the object tag, everything run without a problem.
The problem now is that I need to pass querystring to this page dynamically. I tried to set the attribute data in the javsacript to pass the querystring. The querystring value passed successfully, but the data attribute does not seem to be set. I get a blank page.
pdf.setAttribute("data","test.aspx?PrintReport=yes&SavePDF=yes&AccNum="+AccNum);
Does anyone have a clue how I can set the data attribute dynamically to pass in querystring?
Thanks,
var pdfObj = document.getElementById('pdfDoc');
pdfObj.data="test.aspx?PrintReport=yes&SavePDF=yes&AccNum="+AccNum;
As far as the data attribute you're doing everything fine. Here are some examples:
http://jsfiddle.net/3SxRu/
I think your problem might be more to do with the order of execution. What does your actual code look like? Are you writing over the body onLoad function or something?
Also, I assume using the data attribute is a requirement. HTML5 defines data-*. This attribute isn't really valid. Again, maybe your system requires it.
I suspect that things are happening out of order. Try waiting until the onload event of the window before adding the embed.
Also, I suggest using a script like PDFObject to handle the embedding since it is a reliable way to embed PDF across all the various browsers out there. For example you might have something like the following:
<html>
<head>
<title>PDFObject example</title>
<script type="text/javascript" src="pdfobject.js"></script>
<script type="text/javascript">
window.onload = function (){
// First build the link to the PDF raw data ("bits")
// getQueryStrings assumes something like http://stackoverflow.com/questions/2907482/how-to-get-the-query-string-by-javascript
var queryStrings = getQueryStrings();
var reportNameParamValue = queryStrings["reportName"];
var pdfBitsUrl = "getReportPdfBits.aspx?reportName=" + reportNameParamValue;
// just in case PDF cannot be embedded, we'll fix the fallback link below:
var pdfFallbackLink = document.getElementById("pdfFallbackAnchor");
pdfFallbackLink.href = pdfFallbackLink;
// now perform the actual embed using PDFObject script from http://pdfobject.com
var success = new PDFObject( {
url: pdfBitsUrl;
}).embed();
};
</script>
</head>
<body>
<p>It appears you don't have Adobe Reader or PDF support in this web
browser. <a id="pdfFallbackAnchor" href="sample.pdf">Click here to download the PDF</a></p>
</body>