How to avoid a changePage immediately after ajax post with javascript - ajax

Help!
I am using jquery mobile to submit a JSON string to my rails app via an ajax post. The string contains some data that I send via the following ajax call in a JS file:
$.ajax({
url: "http://" + MyURL + ".json",
type: "POST",
data: JSON.stringify(this.form),
dataType: "json",
contentType: "application/json",
success: onSaveSuccess,
error: onFailure,
complete: onComplete
});
This code works, but as soon as I press the button to submit the form, it changes the page to my home page (NOT EVEN THE PREVIOUS PAGE). I want to set this post up so that it doesn't change pages until I tell it to (in either the success or complete functions). I don't want the user to be thrown around different pages after submitting the post. Instead, I want to show my upload spinner until it is done, and then I will redirect them to a specific page.
Does anyone know how to do this?
My login screen uses a (nearly) identical ajax script, and it does exactly what I want. I am assuming that is because there is no page to go "back" to. I don't understand this behavior, so if anyone does, your advice would be appreciated.

Related

After Ajax request session is expired in php

I have created REST API in codeigniter. This API I am using on my codeigniter website and It's working fine. I have created one JavaScript for call API using ajax. It's also working fine but problem is that after calling ajax If refresh the page or tired to open another page of website it's send to me login page. I don't know why my session is expired ?
I have check in chrome developer tools. After calling ajax clicked page return 302 status.
$.ajax({
url: rest_url,
dataType: 'jsonp',
success: function(data){
//some code
}
});
This problem is caused by session ID regeneration during AJAX requests
If you upgrade your codeigniter , it will be good because we have fixed this error with codeigniter. If you can't , you can get a view in how we have fixed it here Fix of this bug in github. As you can see you just need to change the if condition on this file system/libraries/Session.php. I think that my answer will be usefull for you.

Socket.io, client receiving message multiple times

my website's client works normally when I open it and click on my page's button. It loads dynamically inside a div. In this page I include the javascript file that handles the server messages related to it. So the first time I click the button, the page loads, and server sends data for appending in a div. Everything good so far, but when I click again in the button for loading the page, as soon as the server sends the message it will append twice on the given div, and if I click a third time, the client will append 3 times the same data to the div and so on. It's like the connection is opened multiple times, but I don't know how to handle this.
I load the page with a simple ajax call:
$.ajax({
type: "GET",
url: "items.php",
success: function(msg){
$("#container").html(msg);
}
});
And the include on the items.php:
<script src="js/items.js"></script>
Already tried including the items.js in an outer div, but this way It won't work.

Unable to Successfully Update Page Content via ajax

I have a mobile version of a website built in jQuery mobile and living on a dedicated "m.myurl" subdomain. The full desktop version of the site is in Wordpress. My goal is to have updates entered via Wordpress to the desktop site to show up dynamically on the mobile site as well. I am generating the page content from the individual pages of the desktop site via JSON feeds, and have confirmed that the JSON feeds are good (i.e.: if I enter their URL into a browser, I get a good JSON object). However, the AJAX call on the jQuery mobile site refuses to work. I have debugged to the point that I am not receiving any errors in my console, but when I attempt to log the response from the AJAX call to troubleshoot, it consistently shows in the log as undefined, so I am not receiving known good JSON objects.
The functions I'm using to make the AJAX call are as follows:
function processResponse(response){
var update = $(response).find('content');
$('.content').append($(update).html());
};//ends processResponse
function processJSON(url){
$.ajax({
url: url,
data: null,
success: processResponse,
dataType: 'jsonp',
});//ends ajax
};//ends processJSON
I've set the three JSON URL's equal to variables, but have tried calling the function with the URL passed in instead of the variable, to no effect. The 'processJSON" function is called within the content div of each page section of the jQuery mobile index.html file within a $(document).ready.I'm using 'jsonp' as the dataType, as I assume that calling from a subdomain to a main-level domain would be considered a cross-domain call. That said, I'm trying to get this to work locally, and haven't placed it on a test server yet.
Any assistance is appreciated. Thanks!

getJSON and ajax not working in IE

I am trying to get getJSON working on my IE, but I read that IE doesn't support it that well. So I decided to use ajax instead.
My code is:
$.ajax({
dataType: 'json',
url: 'http://openexchangerates.org/latest.json',
success: function(data)
{
alert('done');
}
});
Thanks
You cannot make crossdomain ajax calls. Take a look at the top post at this SO post. If you have a specific need to do so, you need to use some sort of sockets. This can be file_get_contents in php or another server-side language
I had this problem just the other day. Internet Explorer doesn't like you doing cross-domain requests with AJAX.
I solved it by having a server-side script do the cross-domain API request, and then call the result of that script in my AJAX call.

Is it possible to submit a form to a remote script with ajax?

i have have a form in site A and want to submit the form to a script located in site B.
If i submit the form normally all works well. If i try something like the following all hell breaks loose (well actually, nothing really happens :D)
$('#gs_vote_button').click(function(){
$.ajax({
type: "POST",
url: 'http://www.siteB.com/process_form.php?param1=x;param2=y',
data: $("#gs_vote_form").serialize(),
success: function(data) {
$('#submit_error').html(data);
}
});
return false;
});
So, is it possible to submit a form to a script in a remote site, with ajax? I really need this, please help...
For security reasons, you cannot use AJAX to send a request to a different domain.
Instead, you can make submit normal <form> that targets an <iframe>. (Note that you would not be able to read the response)
Alternatively, if you have control over or cooperation with the remote site, you can use JSONP.

Resources