Firefox extension missing Referer url in ajax header when making ajax calls - javascript

I am trying to make an ajax call to a server and the server needs referer url to identify my request
$.ajax({
url: abc + '/123/xyz/',
cache: false,
type: 'GET',
crossDomain: true,
headers: {
"key": "xxxxxxxxxxx"
}
}).done(function(result) {
executeOnSuccess();
});
The expected result on the server should be that if i execute this line request.getHeader("referer");
I should get the referer url but i get a null, but if i make the same ajax request using chrome extension it works and i get the referer url.
I have been stuck on this for a while now. The other option is to add referer url manually to the ajax header but i was expecting it to work like chrome?
Does any one has any idea about it ?
Thanks in advance.

The browser will always overwrite the referrer. Meaning you can't change the referrer of an ajax call. But you can try!
$.ajax({
url: abc + '/123/xyz/',
type: "GET",
headers: {
"Referer": "Change here reference"
},
success: function (data) {
alert("Success");
},
error: function (data) {
console.log(data);
}
});
Also, note that if you are planning to submit your extension in AMO, you should always use https in your calls to any server.

Related

Getting parse error while fetching text file content in JQuery

I am trying to fetch data from text file which resides on server. I have access of that location and able to see content when I put URL in browser tab.
I am trying to make AJAX call and get file content, but I am getting Error: Uncaught SyntaxError: Unexpected identifier
Code
function logResults(json){
console.log(json);
}
$.ajax({
url: u,
dataType: "jsonp",
jsonpCallback: "logResults"
});
on console,
I tried below code too, but same result,
$.ajax({
type: 'GET',
url: u,
crossDomain: true,
dataType: 'jsonp',
async: false,
headers: {
"Access-Control-Allow-Origin": "*"
},
success: function(succ) {
console.log("Success: ", succ)
},
error: function(err) {
console.log("Error: ", err)
}
});
This code is always going into error block.
You said:
dataType: "jsonp",
But the URL is responding with:
Deployment automatically finished…
Which is not JSONP, it is plain text.
You get the error when it tries to execute the plain text as JSONP.
Don't put wrong information in the dataType field.
async: false,
That's deprecated. Don't use it. It's also pointless since you are using callbacks.
It's also incompatible with JSONP so it is ignored (until you remove the incorrect dataType).
crossDomain: true,
This has no effect unless you are making a:
non-JSONP request
to the same origin
which gets redirected to a different origin
… which is very rare.
headers: {
"Access-Control-Allow-Origin": "*"
},
Access-Control-Allow-Origin is a response header. It doesn't belong the request. Trying to add it to the request will cause extra problems as it will make the request preflighted.
(At least, that would be the case if you hadn't said dataType: 'jsonp' because adding request headers is incompatible with JSONP).
All you need on the client
The only code you need on the client is:
function logResults(result){
console.log(result);
}
$.ajax({
url: u,
success: logResults
});
The server you are requesting the data from will need to use CORS to grant you permission to access it if it is a cross-origin request.
It is because you have added dataType as jsonp, so that it will try to parse the response to JSON and if the response is not a JSON it will throw error.

Create an HTTP request, give it a header and open url in a new tab

I need to create in either Javascript or jQuery an http request. In it I need to put some header fields and then send this all to an external url that is on the same server. The target web page needs to read these headers. In short, I need to make a sort of redirect, but which contains header parameters provided. I am trying to do it in the flowing way but it does not redirect:
var mdata = {roles: roles};
$.ajax({
url: "SendRequest",
type: "POST",
data: mdata,
//crossDomain: true,
dataType: "json"
})
.done(function (data) {
$.ajax({
url: "/OimPortalEmulation",
type: "POST",
beforeSend: function(xhr){xhr.setRequestHeader('name', 'Salvo'); xhr.setRequestHeader('groups', data)}
}).done(function(){})
.fail(function(){alert('Redirect Failed!')});
})
.fail(function (data) {
alert("ajax error! ");
})
.always(function () {
});
This works in as much as it calls the target servlet which prints out successfully the header provided, but it does no open a new tab nor does it show the pag in the current tab. How can I resolve this?
FYI: I am using Java Servlets on the server side.
Edit: I provided further details on the problem posted.

Cross-domain issue with Jquery

I am trying to create a chrome extension which will look up the meaning of input vocabulary from this URL: http://hanviet.org/ajax.php?query=%E6%97%A5&methode=normal
I made an ajax call by using jquery but got an error because of the cross-domain issue: "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access". Ok, I guest that instead of make a request directly to the URL, I need to call it through a proxy pages as below:
$.get("/myproxy.php?query=日&methode=normal", function( data ) {
alert( "Load was performed." );
});
After doing a google search, there is another chrome extension named DHC to makes http request: https://www.sprintapi.com/dhcs.html. and It works perfectly!
I am wondering that does DHC tool also send a request through its proxy or there is another way to make a direct request that I dont know.
Thank you!
$.ajax({
type: "GET",
url: 'URL',
jsonp: 'callback',
dataType: 'jsonp',
data: {},
success: loginSuccess,
crossDomain: true,
error: ajaxFailed,
contentType: 'application/json',
async: false
});
function ajaxFailed(result) {
alert("Failed: " + result.status + ' ' + result.statusText);
}
function loginSuccess(data) {
alert('Result: ' + data.d);
}
If you use the developers tools of chrome on that site, on Network tab you will see that after pressing the sendbutton it loads the content from https://www.sprintapi.com/api/proxy, so yes, it should be using a proxy.
Even more, as you say the Access-Control-Allow-Origin would'nt let they did it on another way I think.

Sending data to server using get request header

I use jQuery to contact my REST service on server side. The URL looks like this:
http://bla.com/?userid=1,2,3,4,5,6...
The userid string could be very long and it could happen that the max url size will be exceeded.
I can't do a post request, so my question is, whether it would be a good solution to send the userid data within the header? Something like this:
$.ajax({
url: 'someurl',
headers:{'foo':'bar'},
complete: function() {
alert(this.headers.foo);
}
});
Would this be possible?
Thanks
Yes you can send header using Ajax request
$.ajax({
type: "GET",
beforeSend: function(request) {
request.setRequestHeader("Authority", authorizationToken);
},
url: "entities",
data: "json=" + escape(JSON.stringify(createRequestObject)),
processData: false,
success: function(msg) {
$("#results").append("The result =" + StringifyPretty(msg));
}
});
you can read more about header on jQuery AJAX page
Also check this example here
This is totally possible, alhough it would be advisable to just use POST, because it was meant to transfer lots of data.
Using headers is a workaround which results in the same results as POST; losing the ability to navigate forward and back without re-submitting (or re-sending headers in this case, will be impossible if the data is gone).

Perform GET request using jquery

I need to perform the following GET request,
telnet somesite.com 80
GET /index.html HTTP/1.0
using javascript, jQuery.
I've tried to follow the instructions in this site in particular the following code:
$.ajax({
url: 'http://somesite.com',
success:function(data){
alert(data);
}
});
but It doesn't work!
Where am I wrong?
Try this one:
$.ajax({
type: "GET",
url: "http://somesite.com",
timeout: 300000,
contentType: "application/json; charset=utf-8",
success: success,
error: failure
});
function failure(response) {
alert(response);
}
function success(response) {
alert(response);
}
by your code im assuming you are doing a cross domain ajax request. Which are automatically blocked by the browser.
you can either use the allow domain header using Cors see this Cross Domain Get Request in JS/JQuery
or switch to JSONP
If you want to perform a cross domain request try this
Working DEMO
You can use this in your head tag
<script src="https://rawgithub.com/IonicaBizau/jQuery-cross-domain-requests/master/js/jquery.xdomainajax.js">
</script>
code
$.ajax({
url: 'http://somsite.com', // Or your web page link
type: 'GET',
success: function(res) {
alert(res);
}
});

Categories

Resources