I am building a Facebook messenger bot. I got to a point where I need to show a webview. This webview does some payment processing and on success, I call a Messenger SDK's function to close the webview and then do an Ajax call to continue messaging the user. Now I have an issue, the webview does not close until the ajax has finished executing i.e. sending the messages to the user. If I place the Messenger close function outside the ajax call, the webview closes but the ajax is not executed. Please how do I close the webview and then continue executing the ajax request.This is what I am currently doing:
$.ajax({
type: 'POST',
dataType: 'JSON',
url: '/api/payment/'+userId+'/'+payRef,
data: 'userId='+userId,
success: function (data) {
console.log(data);
MessengerExtensions.requestCloseBrowser();
}
})
I don't know if that can solve your problem, but you can try this:
$.ajax({
type: 'POST',
dataType: 'JSON',
url: '/api/payment/'+userId+'/'+payRef,
data: 'userId='+userId,
beforeSend: function() {
MessengerExtensions.requestCloseBrowser();
},
success: function (data) {
console.log(data);
}})
In theory, the window will close before the request init.
I don't know if I understood right, but did you try the beforeSend and complete functions from ajax?
$.ajax({
type: 'POST',
dataType: 'JSON',
url: '/api/payment/'+userId+'/'+payRef,
data: 'userId='+userId,
beforeSend: function(jqXHR, settings) {
// Action before send the request to the url
},
success: function (data) {
// Action if the process in the url url don't throw any errors
},
complete: function(jqXHR, textStatus) {
// Action when the request is returned to application
},
error: function(jqXHR, textStatus, errorThrown) {
// I would recommend you always use the error function.
}
})
Related
I have an application which is trying to sent an ajax request to an external api, the request returns with a connection refused error on console. Below is the code I am using along with the image of the actual issue. Any suggestions on this ? or if someone can point me in the right direction, thanks.
$.ajax({
url: url,
type: "POST",
data: args,
timeout: 15000,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (err) {
console.dir(err);
}
});
Console error image
I am new to ajax and javascript.
I have the following web method in a page called people.aspx in the root of my web porject:
[System.Web.Services.WebMethod]
public static string RenderDetails()
{
return "Is it working?";
}
I'm attempting to access the web method via an Ajax call from the people.aspx page. I have the following ajax call on the click event of a div:
$("div.readonly").click(function (e) {
e.stopPropagation();
$.ajax({
type: "POST",
async:false,
url: "people.aspx/RenderDetails",
dataType: "json",
beforeSend: function () {
alert("attempting contact");
},
success: function (data) {
alert("I think it worked.");
},
failure: function (msg) { alert("Sorry!!! "); }
});
alert("Implement data-loading logic");
});
I'm not receiving any errors in the javascript console, however, the ajax call also does not hit the web method. Any help will be appreciated.
Thanks!
Try change the type to GET not POST (this is probably why your webpage isn't getting hit). Also your failure parameter is incorrect, it should be error. Expand it to include all parameters (it will provide more information). In short, change your entire AJAX query to this:
$.ajax({
type: "GET",
async:false,
url: "people.aspx/RenderDetails",
dataType: "json",
beforeSend: function () {
alert("attempting contact");
},
success: function (data) {
alert("I think it worked.");
},
error: function (jqXhr, textStatus, errorThrown)
alert("Sorry!!! "); // Insert breakpoint here
}
});
In your browser, debug the error function. The parameters (particularly jqXHR) contain a LOT of information about what has failed. If you are still having more problems, give us the information from jqXHR (error string, error codes, etc).
I am developing a mobile app using phonegap (JQ + Html ). In my app, consuming REST webservice using AJAX calls.When service invoke, I am showing a progress bar animated GIF image . The problem is, browser freezes when calling AJAX. So the progress bar is not showing.
In ‘beforeSend’ i am showing the progress bar image and after ‘complete’ i am hiding the progress bar image.
I am also trying async: true . But it execute service as asynchronously. In my app, asynchronous execution is not suit. Because asynchronous execution will not wait for ajax executing. My app should wait until the ajax execution complete. In that process time I want show progress bar.
Here is my code.
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
accepts: "application/json",
beforeSend: function() {
StartPBar():
},
data: JSON.stringify(RQ),
async: false,
url: URL,
complete: function() {
stopPBar();
},
success: function(res, status, xhr) {
try {
RS = res;
} catch (e) {
alert(e);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Excpetion " + errorThrown + XMLHttpRequest);
}
});
Any suggestion to show the progress bar stay on screen until the process is fully complete? Any help would be appreciated. Thanks
Make sure you verify your javascript code.
Remove this code.
beforeSend: function() {
StartPBar():
},
Replace your jquery mobile with this one jQuery Mobile 1.4.0-rc.1
http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.js
Replace your code with this one.
$.mobile.loading('show');
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
accepts: "application/json",
data: JSON.stringify(RQ),
async: false,
url: URL,
complete: function() {
$.mobile.loading('hide');
},
success: function(res, status, xhr) {
try {
$.mobile.loading('hide');
RS = res;
} catch (e) {
alert(e);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$.mobile.loading('hide');
alert("Excpetion " + errorThrown + XMLHttpRequest);
}
});
Try to set async: true, the async: false will freeze the browser until the request is completed. Also move the async: true before beforeSend method.
The async: true, when supported by browser, basically means: browser will send data asynchronous and will not block or wait other actions from executing. This is the only in my opinion way to show the progress bar indicator. Because (from the documentation):
Note that synchronous requests may temporarily lock the browser,
disabling any actions while the request is active.
If you want to wait until ajax requests done, you can do it also with async:true like;
StartPBar():
$.when(runAjax()).done(function(result) {
// result conatins responseText, status, and jqXHR
stopPBar();
});
function runAjax() {
return $.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
accepts: "application/json",
data: JSON.stringify(RQ),
async: true,
url: URL,
success: function (res, status, xhr) {
try {
RS = res;
}
catch (e) {
alert(e);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Excpetion " + errorThrown + XMLHttpRequest);
}
});
}
In this example, when ajax request completed, progressbar stop function will be called.
I have a Self-Hosted-Service(using WCF) that will run on clients machines. That service is supposed to make request to another server, get the data as XML then it returns to me that data as JSONP. Now i want to check if the service is running or not .. How can i check that ?
In my JS code i use $.getJSON with callback, so i tried to use .fail like this:
$.getJSON("http://localhost:8080/url?callback=?", function () {
alert("success");
}).fail(function () {
alert('fail');
})
but fail function didn't called when the server is not running(on chrome the Type is pending and Status is Failed)
Then i tried to use $.AJAX like this:
$.ajax({
type: 'GET',
dataType: 'jsonp',
url: 'http://localhost:8080/url?callback=?',
success: function (data, textStatus) {
alert('request successful');
},
error: function (xhr, textStatus, errorThrown) {
alert('request failed');
}
});
I got the same result.
When you make the AJAX request to your localhost and /url? returns weather the other server is up or not, your script won't fail. Because http://localhost/url is online.
I'd make the /url script return JSON array with remoteHostOnline: true or false,
then use:
$.ajax({
type: 'GET',
dataType: 'jsonp',
url: 'http://localhost:8080/url?callback=?',
success: function (data, textStatus) {
if (data.remoteHostOnline == false) {
alert('remote host not online');
}
}
});
You might have to tweak this script I didn't test it but you will understand what's wrong.
i'm working with python and js on a simple website.
i'm trying to call a method from the client side and get result, but no matter what i do
success function isnt happening.
this is my JS
$.ajax({
url: "http://127.0.0.1:8000/api/gtest/",
type: "POST",
data: { information : "You have a very nice website, sir."},
dataType: "json",
success: function(data) {
alert ("post is success");
},
error: function(request,error) {
alert(request.responseText);
alert(error);
}
});
this is my server side code
def gtest(request):
jsonValidateReturn = simplejson.dumps({"jsonValidateReturn": "ddddd"})
return HttpResponse(jsonValidateReturn, content_type='application/json', mimetype='application/json')
The server responds to the call -
"POST /api/gtest/ HTTP/1.1" 200 31
tried to go over similar questions here but with no success :\
no matter what I do, only the error function is called.
the error alert is always empty.. no actual message.
I know this is probably a small change but I can't find it.
$.ajax({
url: "http://127.0.0.1:8000/api/gtest/",
type: "POST",
data: {
'information' : "You have a very nice website, sir.",
'csrfmiddlewaretoken': '{{csrf_token}}'
},
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function(data) {
alert ("post is success");
},
error: function(request,error) {
alert(request.responseText);
alert(error);
}
});
i cant upvote mccannf's comment.
The problem was solved by the link he posted, i ran the html code from a file on my pc and i needed to load it from the server so link wont start with file:// but with http://
best regards..