The page I'm scrapping is detecting whether the request is ajax or simple one. How can I specify that ?
app.get('/gsb', function(res, req){
request({
method: 'POST',
url: "https://www.somesite.com/somepage/"
} ...
Now, this somepage detects whether its xhr or not.
Thanks
AJAX is typically indicated via the X-Requested-With header in the request.
request({
method: 'POST',
headers: {'X-Requested-With': 'XMLHttpRequest'},
url: ...})
Try that and see if it does the trick.
Related
I am trying to send a post request with the following code. But the request goes as GET request, instead of POST. How to fix this.
$.ajax({
url: 'https://www.exampleurl.com',
method: 'POST',
headers: {"Access-Control-Allow-Origin": true},
data: {url:'bla',call:"trans"}
dataType: 'jsonp',
success: function(data){
console.log('succes: '+data);
}
});
This is the error I am getting
XMLHttpRequest cannot load https://example.com. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 401.
When removed the header Access-Control-Allow-Origin, I am getting a 404 error
I don't think, you can use a POST method with jsonp request. jsonp callbacks only for with GET method. Have a look at link .
You don't have to pass parameters in url attribute when you want to send POST request you should use data attribute instead, take a look at jQuery.ajax() :
$.ajax({
url: 'https://www.exampleurl.com',
method: 'POST',
data: {q:1, q2:2},
headers: {"Access-Control-Allow-Origin": true},
dataType: 'jsonp',
success: function(data){
console.log('succes: '+data);
}
});
Hope this helps.
I'm trying to make a GET request through jQuery to the Mailchimp API. It seems though my custom header is not correctly set as I get a Your request did not include an API key. error.
It works fine if I make the request using curl on my Ubuntu machine:
curl --header "Authorization: apikey 709XXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us11" https://us11.api.mailchimp.com/3.0/campaigns
Here's my code:
$.ajax({
type: 'GET',
url: 'https://us11.api.mailchimp.com/3.0/campaigns',
crossDomain: true,
dataType: 'jsonp',
contentType: "application/json; charset=utf-8",
headers: {
'Authorization': 'apikey 709XXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us11'
}
}).done(function (response) {
console.log(response); // verbose
});
I even tried adding this above:
$.ajaxSetup({
headers: { 'Authorization': 'apikey 709XXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us11' }
});
You need to add the key via Basic Auth like and as far I am aware off, You can't query it from front-end, it must be on the back-end.
Find an example in NodeJS:
headers: {
'Authorization': 'Basic ' + new Buffer(`anything:${MailChimpKey}`).toString('base64');
}
MailChimp not allowed to direct access with ajax. Once make Server WebRequest. It will surely work.
I want to pass Authorization header while POSTing data to server.
I tried
$.ajax({
url : <ServiceURL>,
data : JSON.stringify(JSonData),
type : 'POST',
contentType : "text/html",
dataType : 'json',
success : function(Result) {
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', <Authorization Header Value>);
},
error: function (RcvData, error) {
console.log(RcvData);
}
});
But REST service returns error (error code : 500). The same service was working fine with $.post() before adding authorization.
could anyone tell me "How to pass authorization header in $.post()??"
Use
contentType: 'application/json',
You may have gotten data and contentType mixed up.
contentType is the Content-type header you send.
data changes how jQuery treats the data you receive.
The jQuery $.ajax() method accepts a headers value in the settings object.
So:
$.ajax({
// url, data, etc...
headers: {
"Authorization" :"Basic " + myBase64variable,
"Content-Type" :"application/json"
}
});
Source: http://api.jquery.com/jquery.ajax/
PS: Seems you can also pass in a new settings object in the beforeSend parameter. I didn't know this, so thanks for asking this question :)
i am trying to make cross domain request but server can not response with
Access-Control-Allow-Origin: *
i am trying to read response header.
when i am trying to read status code or ready state it returns 0. my code for ajax request is as follows.
$.ajax({
url: "https://accounts.google.com/o/oauth2/auth",
type: "POST",
data: {"response_type":"token","client_id":"6720XXXXXXX.apps.googleusercontent.com","redirect_uri":"http://localhost:51967/oauth2callback.aspx","scope":"https://www.googleapis.com/auth/calendar","state":"this is state information","approval_prompt":"auto"},
crossDomain: true
}).always(function(jqxhr,testStatus,errorThrown)
{
alert(jqxhr.readyState);
alert(jqxhr.status);
});
thanks in advance
have you try this jquery crossdomain plugin?
another way is using jsonp as response type
$.ajax({
url: "https://accounts.google.com/o/oauth2/auth",
type: "POST",
dataType: 'jsonp',
...
I'm losing my custom header when using PUT type with .ajax. But, the header is fine with GET, but gets mangled with PUT. Please see evidence 1:
// GOOD GET:
$.ajax({
url: url,
type: 'GET',
dataType: 'json',
headers: {
Accept: "application/json"
}
});
// Actual header sent (using fiddler):
Accept: application/json
// BAD PUT:
$.ajax({
url: url,
type: 'PUT',
dataType: 'json',
headers: {
Accept: "application/json"
}
});
// Actual header sent (using fiddler):
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
As you can see the only difference is the value of type causing the value of the Accept part of the header gets trashed. jquery-1.8.2.js. Any thoughts? Thanks
Stabby
It seems it is browser related:
http://jsfiddle.net/oceog/WqXzA/
Request URL:http://fiddle.jshell.net/_display/
Request Method:PUT
Status Code:200 OK
Request Headersview source
Accept:application/json
Chrome 25.0