:1 is being appended to my ajax requests with jQuery - ajax

I'm trying to access an external API from my website, and for some strange reason I am getting an ':1' appended to my ajax requests. Everything else seems to be right. BTW, I'm trying to access the Bing Images API using jQuery.
$.get('http://api.bing.net/json.aspx?callback=?',
{
AppId : <MYAPPID>,
Query : 'help',
Sources : 'Image'
},
imageResponseHandler,
'json'
);
I get this URL, which throws a syntax error in chrome console:
http://api.bing.net/json.aspx?callback=jsonp1329103936801&AppId<myappid>&Query=help&Sources=Image:1
The ':1' at the end brings up a 'not expecting token :' error. Where is it coming from? Removing the colon and pasting the url into my browsers gets me the json I want, but this seems to break before actually making the request.
Thanks,
Siegfried

Related

Inconsistent functionality on CommentThreads: insert on Youtube DATA API

i could need some help with the Youtube data API , i have a server in which utilizes the Youtube API in adding a youtube comment using JS , it's quiet simple :
const snippet = {
snippet: {
videoId:videoId,
topLevelComment:{
snippet:{
textOriginal:message
}
}
}
};
axios({
method: 'POST',
url: 'https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&alt=json&access_token='+access_token,
data : snippet
})
This used to work without any issues , but since yesterday, it stopped working , giving me this error :
"code": 400,
"message": "The API server failed to successfully process the request. While this can be a transient error, it usually indicates that the request's input is invalid. Check the structure of the <code>commentThread</code> resource in the request body to ensure that it is valid."
Now the bizarre thing is that , testing in a sandbox online with an axios client , this works , i tried with postman , it works , in the youtube code sample sandbox , it works and i'm able to add the comment using an access token , but in my server , it fails even though all of the data is passing correctly , it fails , even tho it is the exacty same request , giving me that error..
My server is hosted on heroku.
The quota limit is not reached , it is far from reached sine the api is only used once in a while.
I tried passing my API Key , but it's the same with or without it in all the above cases.
I don't know what to do from this point i'm utterly confused.
Any help would be appreciated , thank you.

Why does this CORS request to a google Drive Sheet fail in Firefox ? (works in Chrome)

I'm trying to request a google sheet from the client in javascript, using jquery ajax.
The following code works in Chrome but fails in Firefox.
Question : how can I get it to work in Firefox?
If it's a server configuration issue then does this mean it's impossible to link to google drive documents from a firefox client?
Here is the code:
var url = 'http://docs.google.com/spreadsheets/export?id=1-on_GfmvaEcOk7HcWfKb8B6KFRv166RkLN2YmDEtDn4&exportFormat=csv';
$.ajax({
url : url,
type : 'GET',
dataType : 'text',
success : function(res, status){
console.log('status : ' + status);
console.log(res);
},
error : function(res, status, error){
console.log('status : ' + status);
console.log(res);
console.log(error);
}
});
In Chrome I get a 307 response then a 200 with the desired data.
In Firefox I get a only a 200 response but with the error message something like "Access-Control-Allow-Origin header missing, Same Origin Policy does not allow to fetch this resource".
The problem is that docs.google.com does not set CORS headers on redirects. And Chrome is not following the specification by not enforcing that and therefore has a security bug of sorts.
docs.google.com is in Chrome's HSTS preload list. The request to http://docs.google.com is transparently rewritten to https://docs.google.com, so no redirect happens.
I assume this will resolve itself if Firefox pulls an updated copy of the HSTS preload list. As Anne notes, simply changing the link to https directly will solve your use case.
I found a workaround by configuring Google Drive slightly differently, and using JSONP :
1) In Google Drive, Publish on the web the document & set sharing options to Public
2) Export your data in JSON format with a JSON type link, it will look like : "http://spreadsheets.google.com/feeds/list/YOUR_FILE_ID/od6/public/values?alt=json&callback=myCallback". You need to append &callback=myCallback to use JSONP. You can use jQuery to make your JSONP call.
3) To use the data, you need to define the callback function specified in the url , in this case "myCallback"
I've mentioned a similar procedure in a different answer but I think it can be useful to mention it here as well since it directly relates to the problem I was facing.
#EnricoFerreguti you should replace YOUR_FILE_ID with your file ID. Example : https://spreadsheets.google.com/feeds/list/1-on_GfmvaEcOk7HcWfKb8B6KFRv166RkLN2YmDEtDn4/od6/public/values?alt=json from the http://misterfresh.github.io/react-drive-cms/ website .

Having trouble making an HTTP request to an API using Bigcommerce

So I'm working on Bigcommerce to build a website for a client. since Bigcommerce does not allow the upload of PHP files on their system I'm using AJAX in a script tag in an HTML file. The documentation for the API I'm using says to use my API key in the username field for HTTP Basic Authentication, and that all requests must be transmitted over HTTPS.
So firstly, here's my code
$.ajax({
type: 'POST',
url : "https://www.freightview.com/api/v1.0/rates",
data : ({ myRequest }),
dataType : "jsonp",
Accept: 'application/json',
beforeSend : function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + "MyKey:");
}
})
I've tried doing
Username : 'mykey'
and some other various ways to put the key in there but keep getting
401 Unauthroized
I've searched around for some ways to do this and noted a few possible issues:
Ajax GET request over HTTPS
It's mentioned on this page that AJAX cannot make cross-domain HTTPS requests. Does this mean I have to find another way to make the request? If so, how can I make such a request with the limitations I have?
Is it simply that I'm not properly defining my username for the Authentication? I'm hoping this is the only problem, but I've looked at a lot of questions on this site about Basic Auth in AJAX.
JQuery Ajax calls with HTTP Basic Authentication The user who asks this question seems to have a similar problem to mine, but the answers are quite varied, and I'm not sure if they'll work in my situation or even how to begin implementing them. Do I need CORS? Can I even get CORS to work on Bigcommerce?
So as you can see what I thought would be a relatively simple snippet of code turned out to be a lot more complex than I thought, and I could really use some of that StackOverflow magic!
Edit: grammar and spelling
In BigCommerce you have to encode your credentials with Base64.
Example:
// Define the string
var string = 'Hello World!';
// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"
// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"

Localhost returns 404.3 when fetching json through ajax (Windows 8.1)

So I have been getting the infamous 404.3 error when trying to use AXAJ to access a .json file launching the site (or more of a test app hehe) through WebMatrix on localhost.
Yes, I am aware of the IIS configuration. I am on Windows 8.1(x64), so I had to even turn on MIME types functionality separately. I configured a MIME type for .json with application/javascript. Then I went and added a handler to *.json, pointed it to C:\WINDOWS\system32\inetsrv\asp.dll. I set the verbs to GET and POST (those are what I use in my ajax function). I also tried unchecking the "Invoke the handler only if request is mapped to..." to no avail.
I am using one function to send data to PHP file which writes it to the JSON file and then another to fetch data from the JSON file directly. Writing through PHP works. Fetching doesn't. I am completely at a loss, does anyone have any ideas? The code I am using to fetch the data is your bog-standard ajax:
function getDate(path, callback) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
var data = JSON.parse(httpRequest.responseText);
if (callback) callback(data);
}
}
};
httpRequest.open('GET', path);
httpRequest.send();
}
When I host this on my server space, it works totally fine. But I want to get it to work locally for testing purposes as well.
If writing to the file works but fetching doesn't work. Then you should check for the link of the file.
The error 404 as the name refers to, is an error for the file name. There isn't any other sort of error, even the Ajax request is working fine and giving the error 404 (file not found). So the only thing that you can do is, to make sure that while fetching the data, you use the correct link.
Here can be a help, when you send the Request through Ajax, there is a Network tab in your Browser's console. Open it, and look for the request. It would in red color denoting an error and click it. You'll see that the link you're providing isn't valid.
Look for the errors in the File Link then and update it.
The lengths I go to, to clean up my profile...
When you require a JSON format, or any file for that matter you have to specify in your request what data type you need, IIS will not make any assumptions. So
xhr.setRequestProperty('Content-Type', 'application/json');
is something one must not forget. I set also the X-Requested-With header. Note that to reproduce this issue I used IIS that is installed on Windows 10 Pro, so not exactly the same system (3 years later - holy crap!).

getting jsonp to work in asp.net api

having issues getting my jsonp to work with my client backbone script,pagenator keep gettgin invalid key error
having looked around people say its to to do with my service not return jsonp.
For example SyntaxError: invalid label, i've written a mvc4 web api service this is what it returns does it look right and also is there anything i need to add to my api to support jsonp ?
{
"odata.metadata":"http://test.test.uk/api/odata/$metadata#lnews","value":[
{
"ID":1,"title":"This is a test news artical","mainContent":"<p>\r\n\tthis is a test article</p>\r\n","featured":1,"visiblehomepage":1,"thedatetime":"2013-08-05T10:36:05.98","expireon":"2013-08-15T00:00:00","category":17,"embargo":null,"embargotime":"PT0S","embargodate":null,"customthumbnail":null,"news_layout":3,"LNBE":0,"LNBN":0,"LNBS":0,"LNBW":0,"LNWV":0,"LNWS":1,"LNDY":0,"LNSW":0,"LNSH":0,"LNCV":0
},{
"ID":2,"title":"This is a test article","mainContent":"<p>\r\n\twelcome to the best site in the world</p>\r\n","featured":1,"homepage":1,"thedatetime":"2013-08-05T10:42:54.763","expireon":"2013-08-22T00:00:00","category":null,"embargo":null,"embargotime":"PT0S","embargodate":null,"customthumbnail":"Water lilies.jpg","news_layout":4,"LNBE":1,"LNBN":1,"LNBS":1,"ff":1,"LNWV":1,"LNWS":1,"LNDY":1,"LNSW":1,"LNSH":1,"LNCV":1
}
]
}
Here is a JSONP URL:
http://odata.netflix.com/v2/Catalog/Genres?$format=json&$callback=?
Here is a diagram:
CLIENT SERVER
------ ------
Makes URL request Looks up function matching this URL param ($callback)
Executes asynchonously If found, executes function asynchonously
Returns from callback Returns data from server side as a function, or error
References
Learn By Example Guide for Select State.gov Data (SSD): JSONP

Resources