ExtJS Ajax (JSON) post not working properly - javascript

I have seen a couple of examples from stackoverflow itself and also in the extjs documentation. But all I can see is 404 error.
Ext.Ajax.request({
url: '/project-desktop-service/decisions.json',
method: 'post',
headers: {'Content-Type': 'application/json'},
params: {
rolename: 'rolename'
},
success: function(){console.log('success');},
failure: function(){console.log('failure');}
});
I also have tried different combinations of removing and adding the content type, adding absolute url etc. But the result is the same failure everytime.

from Wiki :
The 404 or Not Found error message is a HTTP standard response code indicating that
the client was able to communicate with the server,
but the server could not find what was requested.
the only thing is not function right is you URL or you request somthing that was not ther.

Related

How do I get around the error TypeError: self._form.on is not a function?

I am using the node js request module to send a post request to a website. After looking at the websites request it is using "Form Data" as a payload. The content type for the page is "application/x-www-form-urlencoded". My script is being run on a button click in an electron app. A sample of my code is as follows:
let options2 = {
method: "POST",
uri: site_url,//predefined
followAllRedirects: true,
headers: {
'Origin': checkout_host,//predefined
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.8',
'Referer': referer//predefined
'User-Agent': UA//predefined
},
formData: {
information that site is requesting
}
request(options2, (err, resp, body) => }
*I end up repeating the code for different information here*
}
When running my code this is the error I encounter and it stops immediately:
TypeError: self._form.on is not a function
It seems the reason that this occurs is that when handling an error, the request module uses the .on method on a FormData object. However this isn't the Browser FormData, rather it's pulled from another dependency.
Node can recognize everything just fine, but the Browser has some trouble with it. I'm assuming that since you're using this for and Electron app, that might be the issue.
See this to check if it helps: https://github.com/request/request/issues/1961#issuecomment-233956542
Otherwise, if this is on a front-end, maybe using the fetch() can be an alertnative.
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
The other hacky option is to go into the request module (specifically request.js) and search for self._form.on and comment out that block. But I do not recommend this at all because it's simply bad practice, doesn't really solve the problem, and can cause unexpected side-effects.

jquery $.ajax gives 404 not found. Im using node.js with express

This jquery:
$.ajax({
method: "post",
data: JSON.stringify(data),
contentType: "application/json",
url: "http://localhost:3000/ajax"
});
is giving error 404 not found. Here is my server side:
router.get('/ajax', function(req, res ,ext){
var strings = ["rad", "bla", "ska"]
console.log('body: ' + JSON.stringify(req.body));
console.log("AJAX RECEIVED");
res.send(strings);
});
so i do have the /ajax route. When i go to http://localhost:3000/ajax im able to acccess the site . However, when I try to access it with the ajax method I get the 404 error. So im wondering if the error could be with my code, or if it could be the firewall on my computer. Im actually using a company computer that has a firewall that blocks certain sites and I cannot disable it.
If you want to do a HTTP POST, use router.post, not router.get.
your $.ajax call is using POST but your route in express.js is only listening for GET requests.
When you hit the URL through the browser it's doing a GET, so it worked.
You would need to set up a route for POST with router.post('/ajax', ...)

Get Index Azure Search Fails 'Response for preflight has invalid HTTP status code 403'

I previously posted this question on how to pull the index schema from azure search.
I was able to pull an index schema by using the GET Index REST API using Postman with no problems, but when running my code in a browser I get the error:
XMLHttpRequest cannot load https://[service name].search.windows.net/indexes/[index name]?api-version=[api-version] Response for preflight has invalid HTTP status code 403
I'm just running the following code in the console:
$.ajax({ url: 'https://[service name].search.windows.net/indexes/[index name]?api-version=[api-version]', type: 'GET', datatype: 'json', headers: {'api-key': [api-key]}})
However, this code works fine (returns all data-- the only difference is /docs):
$.ajax({ url: 'https://[service name].search.windows.net/indexes/[index name]/docs?api-version=[api-version]', type: 'GET', datatype: 'json', headers: {'api-key': [api-key]}})
The other weird thing is that the request headers for the GET Index Request doesn't show the API key.
It seems like CORS (which is setup on Azure Search to '*'), doesn't like the request from a browser for the GET Index?
Any help would be welcome.
Azure Search does not support admin-level operations like Get Index in CORS requests. See this related answer.

UTF8 character encoding using node Request module

I have a simple app that uses a jquery ajax request to send form data to a node server, which in turn submits to a third party api using the Request module for node js.
The issue I'm having is that accented (and other similar) characters are not encoded correctly when they reach the third party server. For example é is recorded as é
I am fairly sure this is to do with the settings for Request as I get the same results when I bypass the ajax call.
Here are the settings I am using:
html:
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
jquery ajax settings:
type : 'POST',
url : '/api',
data : formData, // A json object
dataType : 'json',
ContentType : 'text/html; charset=utf-8'
Request module settings in node (there is nothing happening to the form data between ajax post and being sent by request):
request.post({
url: "https://testurl.com/api/",
form: formData,
headers: {'Content-Type': 'application/json; charset=utf-8'}
} ...
I have read various SO solutions but had no success, so any suggestions greatly appreciated.
After researching character encoding I discovered that the issue is to do with multibyte encoding of various characters (a quick google will find some good SO posts on the subject).
I thought it was odd that Request didn't handle this automatically, so I played around with the Request syntax and managed to fix the issue. Here is my revised code that works:
request(
{
url: 'https://testurl.com/api/',
method: 'POST',
json: true,
headers: {
'content-type': 'application/json'
},
body: formData
},
function (error, response, body) {
...
}
);

How to get a json response from yaler

I create an account with yaler, to comunicate with my arduino yun. It works fine, and i'm able to switch on and off my leds.
Then i created a web page, with a button that calls an ajax function with GET method to yaler (yaler web server accept REST style on the URL)
$.ajax({
url: "http://RELAY_DOMAIN.try.yaler.net/arduino/digital/13/1",
dataType: "json",
success: function(msg){
var jsonStr = msg;
},
error: function(err){
alert(err.responseText);
}
});
This code seem to work fine, infact the led switches off and on, but i expect a json response in success function (msg) like this:
{
"command":"digital",
"pin":13,
"value":1,
"action":"write"
}
But i get an error (error function). I also tried to alert the err.responseText, but it is undefined....
How could i solve the issue? Any suggestions???
Thanks in advance....
If the Web page containing the above Ajax request is served from a different origin, you'll have to work around the same origin policy of your Web browser.
There are two ways to do this (based on http://forum.arduino.cc/index.php?topic=304804):
CORS, i.e. adding the header Access-Control-Allow-Origin: * to the Yun Web service
JSONP, i.e. getting the Yun to serve an additional JS function if requested by the Ajax call with a query parameter ?callback=?
CORS can probably be configured in the OpenWRT part of the Yun, while JSONP could be added to the Brige.ino code (which you seem to be using).
I had the same problem. I used JSONP to solve it. JSONP is JSON with padding. Basically means you send the JSON data with a sort of wrapper.
Instead of just the data you have to send a Java Script function and this is allowed by the internet.
So instead of your response being :
{"command":"digital","pin":13,"value":0,"action":"write"}
It should be:
showResult({command:"analog",pin:13,value:0,action:"write"});
I changed the yunYaler.ino to do this.
So for the html :
var url = 'http://try.yaler.net/realy-domain/analog/13/210';
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'showResult',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.dir(json.action);
},
error: function(e) {
console.log(e.message);
}
});
};
function showResult(show)
{
var str = "command = "+show.command;// you can do the others the same way.
alert (str);
}
My JSON is wrapped with a showResult() so its made JSONP and its the function I called in the callback.
Hope this helps. If CORS worked for you. Could you please put up how it worked here.

Categories

Resources