I'm trying to make a simple Ajax call in my app on Openshift. This is my ajax call which is triggered by pressing a button:
$.ajax({
url: 'http://my-site-name.rhcloud.com/asciimo',
method: 'POST',
data: {attr:"value"}
});
And this is node in my server.js file:
self.createRoutes = function() {
self.routes['/asciimo'] = function(req, res) {
res.send('done');
};
};
Everything works when I go to my-site-name.rhcloud.com/asciimo, but if I click a button (to get there) I get:
POST http://my-site-name.rhcloud.com/asciimo 404 (Not Found)
even though the link clearly works on it's own.
Change your method to GET
method: 'GET',
When you go to the URL directly in your browser, you are issuing a GET request, not a POST.
I know what the problem was now. In ajax there is a POST request and self.routes['/asciimo'] (given as a template by Openshift) deals with GET requests only. What solved the problem was simply rewriting my function as a separate POST function:
self.addpost = function() {
self.app.post('/asciimo', function(req, res){
res.send('done');
});
};
Related
Due to my starting level with React and Node/Express and, above all, time restriction, I'm developing a single page application without database, so I have only static pages.
I've configured react-router to have clear url, using the hash (http://localhost:8080/#/contacts) i avoid also server calls:
let history = createHistory({
queryKey: false
});
I've got the necessity to send mail from my form. To do this I've found nodemailer and I need to invoke it from an ajax call.
Just to do some pratique with React i've configured my ajax call in my componentDidMount function:
openForm() {
$('#contacts-form').on('submit', function(){
$.ajax({
url: '/mail',
type: 'post',
data: { msg: $('#data').val() },
success: function(data){
console.log(data);
},
error: function(){
console.log('err form');
}
});
return false;
});
}
componentDidMount() {
this.openForm();
}
Now, after I've installed body-parser and set my form's action to "/mail", in my server.js I've configured my .post route
app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({
extended: true
}));
app.post('/mail', function(req, res){
res.send('ajax call recived');
});
So now once I try to submit my form, my ajax call fails, in the console, I get this error:
POST http://localhost:8080/mail 404 (Not Found)
err form
It's the first time I'm working with these tools, where I'm doing wrong?
I have a button in my webpage which calls an ajax method a s below
$.ajax({
cache: false,
type:'POST',
data: 'type='+userType +'&user='+user ,
url:' ".\yii\helpers\Url::to([$program.'/'.$url.'/setcustomer/'])." ',
success: function(data) {
console.log('Hii');
$('#phoneErr').html(data);
}
});
This works in all browsers except IE11
I get the following error when i click on the button:
SCRIPT7002: XMLHttpRequest: Network Error 0x800c0008, The download of the specified resource has failed.
Has anyone faced this issue and what is the solution to this?
There is a redirection in my PHP code in the setcustomer action. Can this issue be related to it?
My ajax response body says Key Value
Response HTTP/1.1 302 Found
and not actually redirecting to the required page
is the problem related to IE ajax cannot handle 302 redirect within an ajax response as success.
I make the next solution:
in js:
$(document).ajaxComplete(function (event, xhr, settings) {
var url = xhr && xhr.getResponseHeader('X-Redirect');
if (url) {
window.location = url;
}
});
in php (yii2) (ie not understand 302 via ajax, we send 200) :
$this->redirect(['index', 'id' => $model->id], 200);
I have a simple setup for a resource, currently in routes.rb I have:
resources :leave_requests
This works exactly as expected, on the index view I have a datatable setup using Ajax via a GET, this is also working but the URI is getting very large during the Ajax request. I would prefer this to be a POST action, for this to work I require a POST instead of a GET on the index action in my routes.
However, this will break the CREATE action i.e. will simply load the index page on submitting a new request. i.e. If I do this:
post '/leave_requests', to: 'leave_requests#index'
resources :leave_requests
How can I get these to co-exist happily?
Have you try to do something like in this Rails , Ajax , Post Function , Jquery the code looks something like
$('form').submit(function()
{
var myForm = $('form').serialize();
$.ajax
({
url:'/leave_request/create',
type:"POST",
dataType:'json',
data: myForm,
processData:false,
success: function (msg)
{
alert(msg);
},
error: function (xhr, status)
{
alert(xhr.error);
}
});
});
you can also take a look at jQuery post to Rails
I have a JQuery Mobile app. This app is currently running on localhost. I am relying on some services hosted on some of my backend servers. When I run a query, I noticed in fiddler that I am getting a 302. This 302 is causing my service call to fail.
If I copy-and-paste the URL of the service in my browser window, everything works fine. Here is my JQuery code
$.ajax({
url: GetServiceUrl(),
type: "GET",
data: vm,
contentType: "application/json",
success: service_Succeeded,
error: service_Failed
});
function service_Succeeded(result) {
alert("received!");
}
function service_Failed() {
alert("oops");
}
function GetServiceUrl() {
var url = "http://www.mydomain.com/services/endpoint";
return url;
}
Why am I getting a 302 from my app?
Thank you
your service is sending a redirect request. Maybe you require a login?
guys. I have a juerymobile multi-page, and I have a button in #page-index, when click it, will send a ajax request to server, and changepage to #page-column, It run will in PC, but when i deploy the multi-page in phonegap, the button click can just run only twice, code is below:
function test()
{
$.mobile.changePage('#page_column');
$.ajax({
url: "http://192.168.168.120:8090/fcmobile/getTest",
dataType: "json"
}).done(function(data) {
alert(data.content);
});
}
I found if I remove $.mobile.changePage('#page_column');, the ajax request can be run well any times. but when I add the changePage code, it only can be run twice, in third time, ajax request can't even be send. Dose anybody know reason?
AJAX is made to be asynchronous, so no need to set async to false to get it working. Use events instead.
For example:
function test () {
$.ajax({
'url': "http://192.168.168.120:8090/fcmobile/getTest",
'dataType': 'json',
'success': function (json_data) {
$(document).trigger('test_json_data_loaded');
console.log(data);
}
});
}
$(document).on('test_json_data_loaded', function () {
$.mobile.changePage('#page_column');
});
When you set async to false, you're basically making it so that every time this AJAX request is made, the user will have to wait until all the data is fully loaded before the application/website can do/execute anything else...not good.