Why second request fail? (do not pass CORS) - nginx

When I make CORS request from 127.0.0.1:
return jQuery.ajax({
'type': 'GET',
'url': url,
xhrFields: {
withCredentials: true
},
ContentType: 'application/json',
});
Then everything is OK:
But when I change first letter of ContentType to lowercase as next:
return jQuery.ajax({
'type': 'GET',
'url': url,
xhrFields: {
withCredentials: true
},
contentType: 'application/json',
});
Then request is failed:
I suppose that nginx compares headers case sensitively.
Is this a case? or something different happened... =(

The point is that your browser doesn't send Content-Type: application/json in the second case. The NGINX isn't involved in that at all, your browser sends the Content-Type in both cases, but when you give the ContentType parameter to jQuery, it's being silently ignored. The correct parameter is contentType, not ContentType.

Related

Google Closure Compiler warns of "expressions are not callable"

In the following code, I get the warning:
expressions are not callable
I am using the Google Closure Compiler. The warning occurs when the request object is called as a function. How can I get rid of this warning?
var request = require('request'); // See https://github.com/request/request
request({
url: "https://www.googleapis.com/oauth2/v4/token",
method: "POST",
json: false,
body: tokenPostData,
headers: {
"content-type": "application/x-www-form-urlencoded"
},
}, function (error, response, body) {
});
Figured out the solution. Just add "call" after the request object and make sure the first parameter value is "this".
var request = require('request'); // See https://github.com/request/request
request.call(this, {
url: "https://www.googleapis.com/oauth2/v4/token",
method: "POST",
json: false,
body: tokenPostData,
headers: {
"content-type": "application/x-www-form-urlencoded"
},
}, function (error, response, body) {
});

HTTP Status 403 - Bad or missing CSRF value but the csrf token is set

I have an ajax request which looks like this:
$.ajax({
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
'url': defaults.addToCartUrl,
'data': JSON.stringify({CSRFToken: Config.CSRFToken,currentUser: currentCustomer, entries: cartItems}),
'type': 'POST',
'dataType': 'json',
beforeSend: function(xhr) {
xhr.setRequestHeader('X-CSRFToken', Config.CSRFToken);
},
'success': function (data, textStatus, jqXHR) {
},
'error': function (jqXHR, exception, m) {
console.log('Cannot move products from
}
});
The problem is that I keep getting this HTTP Status 403 - Bad or missing CSRF value but I set the token as a parameter in the data payload as well as on the request header.
Isn't the beforeSend supposed to set the token to "X-CSRF-Token"? Maybe use ajaxSetup for your headers?
Example:
$.ajaxSetup({
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
}
});

meteor can't access cross origin

i got a problem in meteor when i try to make a cross origin call.
when i make the call using Ajax.
$.ajax({
type: 'GET',
url: signoutUrl,
async: false,
contentType: "application/json",
dataType: 'jsonp',
success: function (nullResponse) {
console.log('success');
},
error: function (e) {
console.log('error in HTTP :: >>>>' + JSON.stringify(e));
}
});
it works fine with no problem. but when i am using meteor's HTTP.call method for the same Http request it sent me the error.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
i set the parameters and header in meteor Http.call are
HTTP.call(method, URL,
{params: {
async: false,
contentType: "application/json",
dataType: 'jsonp'},
headers:{'Access-Control-Allow-Origin':'https://www.google.com/*'}
}, function (err, result) {}
but when i check the request. i found the header is like
access-control-request-headers:access-control-allow-origin
so, help me where i am wrong in this HTTP request and how to resolve it

Meteor HTTP.post not working with Trello API

I'm trying to create a webhook through the Trello API by using Meteor's HTTP.post method like this:
HTTP.post('https://api.trello.com/1/webhooks?key=...&token=...', {
params: {
idModel: '...',
callbackURL: '...'
},
}, function(error, result) {...});
The request works but the response i get is "Invalid value for idModel". However, if i try the same request using jQuery:
$.ajax({
type: 'POST',
url: https://api.trello.com/1/webhooks?key=...&token=...,
data: {
idModel: '...',
callbackURL: '...'
},
});
Everything works fine (i.e. the webhook is created and data is returned). Somehow Meteor's method seems to make it impossible for Trello to parse the idModel field. Any ideas what might be behind this? Am i doing something wrong or is there a bug?
I solved it by setting the Content-Type header to application/x-www-form-urlencoded. It was sent as text/plain before.
HTTP.post('https://api.trello.com/1/webhooks?key=...&token=...', {
params: {
idModel: '...',
callbackURL: '...'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}, function(error, result) {...});
Try using data instead of params:
HTTP.post('https://api.trello.com/1/webhooks?key=...&token=...', {
data: {
idModel: '...',
callbackURL: '...'
},
}, function(error, result) {...});
Supplementing this page because I had the same problem doing this in golang. I solve it by adding
req.Header.Add("Content-Type", "application/json")
It is confusing that the 400 message is about an invalid idModel.

How to get remote JSON to work with Kendo grid in ASP.NET

I can successfully make the AJAX call to my service with the following code:
var serverData = { "ZoneParent": "123" };
var request = $.ajax({
type: "POST",
url: "/./Services/Reports.svc/getZones",
contentType: "application/json",
dataType: "json",
jsonp: null,
jsonpCallback: null,
data: JSON.stringify(serverData)
});
request.done(function (msg) {
alert(JSON.stringify(msg));
});
request.fail(function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
However, when I try to implement the same call with my Kendo grid I get an error
The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'
for getZones. My service call work fine with DataTables, but I want to switch to Kendo potentially. I have messed with this for days to no avail. The application is not MVC. Here is my Kendo code snippet:
var dataSource = new kendo.data.DataSource({
transport: {
read: {
url: "/./Services/Reports.svc/getZones",
dataType: "JSON",
data: { zoneParent: "123" },
type: "POST"
},
parameterMap: function (data, operation) {
return kendo.stringify(data);
}
},
schema: {
data: "d"
}
});
var grid = $("#allGrids").kendoGrid({
dataSource: dataSource,
height: 200
});
As cfeduke made similar suggestion you can try to add contentType to the read object of the transport read config just as you did in the $.ajax call.
e.g.
var dataSource = new kendo.data.DataSource({
transport: {
read: {
url: "/./Services/Reports.svc/getZones",
dataType: "json",
contentType: "application/json",
data: { zoneParent: "123" },
type: "POST"
},
parameterMap: function (data, operation) {
return kendo.stringify(data);
}
},
It sounds like the server's reply "Content-type" header is something other than the expected "application/json".
You can use cURL:
curl -v -H "Content-type:application/json" -H "Accept:application/json" \
http://localhost/Services/Reports.svc/getZones
to invoke the endpoint and check the returned header values (-v is verbose, you won't see the headers without it).
Sometimes just setting an "Accept: application/json" header is enough to reveal the problem - either the server coerces the output into JSON or it throws an error that can be tracked down.
I am investigating if there is a another way around this. But seems like Kendo has many limitations and this is one of them. Datables doesn't need a header, just the JSON format.
This is what you need to add to your controller that is sending the data (in case its ajax call)
header("Content-type: application/json");
I wish it wouldn't be like this but Kendo forces this I believe. I prefer datatables, much more freedom and you can customize more.

Resources