Ajax don't return response - javascript

Hello i trying to to save response from htmlpage and save the content of this page, but i always getting an error.What i am doing wrong?
<script type="text/jscript">
var page = "";
$.ajax({
url: "https://www.bbvanetcash.com/local_kyop/KYOPSolicitarCredenciales.html",
type: "GET",
cache: false,
crossDomain: true,
cache: false,
contentType: "application/json; charset=utf-8",
data: {},
jsonp: 'jsonCallback',
dataType: "jsonp",
success: function (data) {
page = data;
console.log("Good");
},
error: function (e) {
console.log("Error");//here where i am stuck
}
});
</script>

"https://www.bbvanetcash.com/local_kyop/KYOPSolicitarCredenciales.html" - i think its URL belong from other domain not from your home/base domain url. that's why you getting error.
You can not make a AJAX http request for NON HOME url.
NON HOME: Its means you can only access your Base domain content though ajax.

Related

Jquery: Redirect Page

$.ajax({
type: "POST",
url: "http://localhost:8080/ChecktToken",
data: {"token": ($.cookie("uid")) },
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function() {
},
error: function(data) {
location.href =('login.html')
},
});
The above Ajax call is used for redirecting the user to the login page if he is trying to access any other pages before actually logging into an application.
This works completely OK, but it takes about 2 seconds to redirect to the login page.
So for example if I try to access http://localhost:8080/index.html prior to logging in I will be able to see that page for 2 seconds before actually being redirected.
Is location.href =('login.html') the best method for redirection or is there something better I could do?
Looking for advices.
2 second delay in redirecting page is due to Ajax call waiting for response from server. These 2 seconds may increase if network of server slows down. To prevent page load you have hide page till you get the response or error. You can do that using beforeSend call back in ajax.
$.ajax({
type: "POST",
url: "http://localhost:8080/ChecktToken",
data: {"token": ($.cookie("uid")) },
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
beforeSend: function() {
//Hide page till you get the response. You can use element Id which holds entire body.
$(body).hide();
},
success: function() {
$(body).show();
},
error: function(data) {
location.href =('login.html')
},
});

javascript call web api from remote server

I have this code
but it run on localhost successfully but when I tried it on remote it refused
var parameters = { domainGuid:"test1",type:"testtype" };
var url = "http://remote_server/api/controller_name/test_api";
// jQuery.support.cors = true;
$.ajax({
url: url,
type: 'POST',
data: parameters,
async: false,
dataType: "json",
crossDomain: true,
success: get_all_videos_success,
error: get_all_videos_error
});
it go to error function every time
Have you tried switching your dataType from json to jsonp?

avoid showing values in url with jquery ajax

I am making ajax call to login with spring security but it shows username and password in the url no matter what.
ex: /?j_username=s&j_password=s
I am trying to find my mistake for a long time but I couldnt be able to see it. It is probably a small mistake.
here is my ajax call;
function performLogin() {
var j_username = $("#j_username").val();
var j_password = $("#j_password").val();
$.ajax({
cache: false,
type: 'POST',
url: "/login",
crossDomain: true,
async: false,
data: { 'j_username': j_username, 'j_password': j_password},
dataType: 'json',
beforeSend: function (xhr) {
xhr.setRequestHeader("x-ajax-call", "no-cache");
}
});
}
Thanks
EDIT:
It is resolved by adding `return false;`
But I am not sure if my approach is good. Here is the update;
'function performLogin() {
var j_username = $("#j_username").val();
var j_password = $("#j_password").val();
$.ajax({
cache: false,
type: 'POST',
url: "/Mojoping2/login",
crossDomain: true,
async: false,
data: { 'j_username': j_username, 'j_password': j_password},
dataType: 'json',
beforeSend: function (xhr) {
xhr.setRequestHeader("x-ajax-call", "no-cache");
},
success: window.location.reload()
});
return false;
}
It has nothing to do with the Ajax call. You are not cancelling the form submission!
function performLogin() {
var j_username = $("#j_username").val();
var j_password = $("#j_password").val();
$.ajax({
cache: false,
type: 'POST',
url: "/login",
crossDomain: true,
async: false,
data: { 'j_username': j_username, 'j_password': j_password},
dataType: 'json',
beforeSend: function (xhr) {
xhr.setRequestHeader("x-ajax-call", "no-cache");
}
});
return false;
}
and however you are adding the event
onsubmit="return performLogin();
If you are using jQuery to attach the event, you can use
function performLogin(evt) {
evt.preventDefault();
...
I created a jsfiddle to test your code and have been unable to replicate your bug. The POST request appears to submit normally (going via the chrome dev tools), and no extra GET values are appearing for me.
Demo: http://jsfiddle.net/5EXWT/1/
Out of interest, what version of jQuery are you using?
{this is just here so i can submit the jsfiddle}

How to set cookie value with AJAX request?

I want to set a cookie value on an AJAX request but the code below doesn't work.
$.ajax({
type: "GET",
url: "http://example.com",
cache: false,
setCookies: "lkfh89asdhjahska7al446dfg5kgfbfgdhfdbfgcvbcbc dfskljvdfhpl",
crossDomain: true,
dataType: 'json',
success: function (data) {
alert(data);
});
How can I set cookies in the header?
Basically, ajax request as well as synchronous request sends your document cookies automatically. So, you need to set your cookie to document, not to request. However, your request is cross-domain, and things became more complicated. Basing on this answer, additionally to set document cookie, you should allow its sending to cross-domain environment:
type: "GET",
url: "http://example.com",
cache: false,
// NO setCookies option available, set cookie to document
//setCookies: "lkfh89asdhjahska7al446dfg5kgfbfgdhfdbfgcvbcbc dfskljvdfhpl",
crossDomain: true,
dataType: 'json',
xhrFields: {
withCredentials: true
},
success: function (data) {
alert(data);
});

Response/callback for JSONP using jQuery $.ajax not working

I am unable to catch response from C# to jQuery using $.ajax. I get an error "SCRIPT ERROR". How can I catch response using JSONP? This is what i am using:
$.ajax({
async: true,
context: mrq,
cache: false,
type: "GET",
url: MYURL,
crossDomain: true,
dataType: 'jsonp',
data: MYDATA,
processData: false,
jsonp: "jsonREQ",
jsonpCallback: "onJSONPsuccess",
success: function (jsonText, textStatus) {}
});
As far as I understand, dataType: 'jsonp' means that as soon as it's returned it's put into the callback function as an argument. So, I'd try this:
onJSONPsuccess = function(response) {
// do something with response, e.g.
results = response["results"]
}
$.ajax({
crossDomain: true,
dataType: 'jsonp',
data: { /*params you're sending in*/ },
jsonp: "jsonREQ",
jsonpCallback: "onJSONPsuccess",
success: onJSONPsuccess
});
You say the server side is C# - are you using WCF? There's a great article here about it:
http://bendewey.wordpress.com/2009/11/24/using-jsonp-with-wcf-and-jquery/
Basically you need to set up WCF (or whatever server-side code you're using) to return the json wrapped in a call to your callback function.
However, with jquery, you can simply add "?Callback=?" to your URL, change the dataType to 'jsonp', and forget about the rest of that stuff. You don't need the jsonp or jsonpCallback options set.
In contrast to a json request, the jsonp request will return your data not wrapped in a 'd' property, so your call back function is:
function(data) { var a = data.myProperty ... }
rather than
function(data) { var a = data.d.myProperty ... }
and the whole method is along the lines of:
var url = configuration.serviceUrl + "/" + method + "?callback=?";
var options = {
type: 'GET',
url: url,
data: args,
dataType: "jsonp",
contentType: "application/json",
success: function(data) {
if (callback != null) callback(data);
}
};
if (typeof errorCallback != 'undefined')
options.error = errorCallback;
$.ajax(options);

Categories