Handling AJAX Origin errors, caused by the Chrome Extension - ajax

Is there way to handle AJAX Origin errors from Chrome's extensions ?
Denying load of
chrome-extension://kldbdjcbjohfhddpicldkbifbkcdanid/data.json.
Resources must be listed in the web_accessible_resources manifest key
in order to be loaded by pages outside the extension.
I tried this:
$.ajax({
url: 'chrome-extension://kldbdjcbjohfhddpicldkbifbkcdanid/data.json',
datatype: 'json',
success: function(xhr) {
alert('ok');
},
erro r: function(xhr, status, err) {
alert('status: ' + xhr.status + ' - ' + err);
}
});
but got nothing in variables. error status is "0", error message is empty.

You can try using the jsonp format. This often gets around cross-origin errors.
$.ajax({
url: 'chrome-extension://kldbdjcbjohfhddpicldkbifbkcdanid/data.json',
datatype: 'jsonp',
success: function(xhr) {
alert('ok');
},
erro r: function(xhr, status, err) {
alert('status: ' + xhr.status + ' - ' + err);
}
});

Sounds like you are attempting to load this resource from a content script, based on the error.
You have to explicitly add either the individual extension resources, or patterns that match your extension resources, in the manifest via web_accessible_resources. Your error message states this is the solution.
Once you've done this, you can construct the URL using chrome.extension.getURL(resourcePath).
Here's an excerpt from my manifest:
"web_accessible_resources" : [
"*.html"
]
And here's the code I'm using to request my HTML template file:
var url = chrome.extension.getURL("template.html");
$.get(url,function(data, textStatus, jqXHR){
console.log("Got the template!");
console.log(data);
},"html");

Related

How do you make an Ajax call to Google Drive API?

My code can get an auth2.0 token...but the examples I've found don't show the final piece of taking a GET, provided here, and turning it into a working request.
Here is the specific URL from that link:
GET https://www.googleapis.com/drive/v3/files/fileId
Here is my AJAX call with the attempt at getting the token (that works) into a hardcoded fileId that I know my Google account has access to.
$.ajax({
type: "GET",
beforeSend: function(request) {
request.setRequestHeader("Authorization", "Bearer" + token);
},
// url: "https://www.googleapis.com/drive/v2/files/18qxc3YgnQ_Yg8n4Q18WCZahE9EPtOZWhoKJuAx6SEHI/permissions",
url: "https://www.googleapis.com/drive/v3/files/18qxc3YgnQ_Yg8n4Q18WCZahE9EPtOZWhoKJuAx6SEHI",
dataType: 'application/json',
processData: true,
success: function(msg) {
console.log('Got File Metadata: ' + msg) + console.log(msg);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('Error: ' + errorThrown + ' / ' + textStatus) + console.log(jqXHR);
}
});
I don't understand how to include the token and the pages and pages of Google documentation do not include any examples of going this route.
I get "invalid credential" errors in the server response.
I don't want to use GAPI because I ran into invalid cookie issues running locally and my code is in a Chrome Extension and it isn't clear whether that approach will work there.
Thank you for any help or direction.
In your code, this line:
request.setRequestHeader("Authorization", "Bearer" + token);
You should have a space after Bearer
request.setRequestHeader("Authorization", "Bearer " + token);

Web Core API Post request get blocked, sometimes

I have this AJAX call to my Web Core API;
saveProperty = function (url, data, callback, errorCallback) {
$.ajax({
async: true,
type: "Post",
url: url,
data: data,
contentType: "application/json",
dataType: "text"
})
.done(callback)
//.fail(errorCallback);
.fail(function (jqXHR, textStatus, errorThrown) {
alert("Something went wrong: " + textStatus + " " + errorThrown);
errorCallback();
});
}
Sometimes it works, more often it does not.
When I use Fiddler and it works, I see
And when it doesn't I see;
EDIT - I can be more helpful now in describing this problem.
if I have Fiddler up and running before I lanch the aplication in development, I do not get this bug.
Otherwise I do get this bug.
Why is that? I would love to know.
The error is HTTP Error 401.2 - Unauthorized
Why would this happen with the same code?

Ajax call to another website

For some reason when I run this simple javascript snippet I get the message "Error! Status = 404 Message = error"
callPHP(1)
function callPHP(args)
{
$.ajax({
url: "http://lectiogroupgenerator.esy.es/index.php",
type: 'post',
data: { "json" : JSON.stringify(args) },
dataType: "json",
success: function (data)
{
if (data)
{
alert(data);
return data;
}
else
{
alert("Data is empty");
}
},
error: function (xhr)
{
alert('Error! Status = ' + xhr.status + " Message = " + xhr.statusText);
}
});
return false;
}
My PHP file is just:
<?php
?>
I'm guessing 404 implicates that the php could not be found, but it does exist and I have no clue why it can't find it maybe it has something to do with me making a google chrome extension?
It might be because of the CORS issue. http://lectiogroupgenerator.esy.es/index.php doesn't allow cross origin HTTP requests.
If that's not the case try explicitly defining the website in the permissions in the manifest file to allow requests in and out to that website.
The problem was caused by the Same-origin policy, it was solved when I got an SSL certificate for my website.

jquery ajax calls gives 404 for Windows phone

I build my software with Jquery mobile. In my code I use Ajax JSONP calls to retrieve information from a server. This gives no problem when testing in my browser. With phonegap / cordova I have generated an Android APK and a Windows XAP file. The Android build gives also a success, but when running on a Windows phone I get a 404 error.
Does anybody have a clue what could be the reason for this 404 error; could it have to do with permissions?
Many thanks in advance.
In the config.xml I have added: access origin="*"
The Ajax call looks like:
function retrieveProvincies()
{
$.ajax({url: 'http://www.itclubsupport.nl/test/AppPHPtest/AppIngang.php',
method: 'POST',
data: {
indicatieTestenApp: 'yes',
soortRequest: 'retrieveProvincies',
klantnaam: 'klantITclubsupport'
},
dataType: "jsonp",
success: function (response) {
alert('works!');
},
error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
alert('Status ' + jqXHR.status + ' received when retrieving provincies. Error: '
+ textStatus + '; ' + errorThrown);
}
})
}

Json of jquery form plugin cannot be acquired

I will upload the file by using jquery form plugin, and question on the phenomenon of process of $.ajaxForm becoming timeout when the response data is json.
Processing by the servers end is done without trouble, and the response data is returned in the shape along as long as it sees with firebug the json form.
However, I think that $.ajaxForm cannot acquire data. Hereafter, it is a part of processed code.
code
$('#upload').ajaxForm({
cache: false,
dataType: 'json',
type: 'POST',
error: function(xhr ,status ,error ) {
alert('error occured. Status:' + status
+ ' --Status Text:' + error
+ ' --Error Result:' + xhr.statusText);
},
timeout: 1000,
dataType:'json',
data:{ 'path':'path' , 'type':'type' },
complete: function(){
alert('complete');
},
success:function(data){
alert('success');
},
});
response
(firebug)
header
Connection close
Content-Length 155
Content-Type application/json; charset=utf-8
Status 200
data
json
{"type":"json","message":"complete process"}
<i>(A browser)</i>
①download json data
②alert('error occured. Status:timeout --Status Text:timeout --Error Result:n/a')
③alert('complete')
</pre>
When dataType was html, success was able to be processed.
Moreover, when it is $.ajax, json becomes success.
Are there a settlement plan?It asks suitably.
try setting up the contentType like
$('#upload').ajaxForm({
cache: false,
dataType: 'json',
contentType:"application/json; charset=utf-8",
type: 'POST',
error: function(xhr ,status ,error ) {
alert('error occured. Status:' + status
+ ' --Status Text:' + error
+ ' --Error Result:' + xhr.statusText);
},
timeout: 1000,
data:{ 'path':'path' , 'type':'type' },
complete: function(){
alert('complete');
},
success:function(data){
alert('success');
},
});

Resources