URL for result of AJAX - ajax

I have AJAX for sort.
$.ajax({
url : orderby_params.ajaxurl,
data : data,
type : 'POST',
beforeSend : function(xhr){
},
success : function(data){
if(data){
$('.list-thumbs').html(data);
}
}
});
Everything works fine.
How can I get URL for AJAX result?
Such as: https://domen.com/shop/?orderby=price

Related

Use variable out of ajax

How I can use count variable out of ajax? In ajax function "count" shows count, but dessous nothing.
var count;
$.ajax({
cache : false,
dataType: 'json',
type : "POST",
url : "count.php",
success : function(tdata){
count = tdata;
console.log(count); //this works
}
});
console.log(count); //this doesn't work
$.ajax() is async, you need to wait for it to finish.
var count;
$.ajax({
cache : false,
dataType: 'json',
type : "POST",
url : "count.php",
success : function(tdata){
count = tdata;
console.log(count); //this works
}
})
.done(() => {
// this code runs after ajax is resolved
console.log(count);
});
Refer to http://api.jquery.com/jQuery.ajax/ for other chaining methods

Cross Domain AJAX call not working in IE11

I am trying a make a cross domain AJAX call. It works in Chrome and Firefox but not in IE11. IE11 seems to drop the AJAX call. IE11 Developer Tools Network reveals that a request is not even made to the url. Here is snippet of code I have.
$(function() {
var url = "https://example.com?abc=xyz";
$.ajax({
type : "GET",
contentType : "text/plain",
url : url + "&callback=?",
dataType : 'jsonp',
xhrFields : {
withCredentials : false
},
headers : {},
success : function() {
console.log("success");
},
error : function() {
console.log("error");
},
complete : function() {
console.log("complete");
}
});
});
$.ajax({
type : "GET",
cache: false,
or before ajax call
$.ajaxSetup({ cache: false });
$.ajax({
type : "GET",

What i am doing wrong here?

Hey I don't know why $(this) not working in my ajax code.I want to append response in #feedback element.
here is my html structure image
AJAX
$("form#userComment").on("submit",function(e){
e.preventDefault();
$.ajax({
url : "request/postComment.php",
type : "POST",
data : new FormData(this),
dataType : "text",
contentType : false,
processData : false,
beforeSend : function(http){
$("#upload").val("Posting..");
$("#comment").val("");
},
success : function(response,status,http){
var text = response.split(" ");
$("#upload").val("Post");
if(text[3] === "'error'"){
$(".response").html(response);
$(".response").slideDown();
}else{
$(this).prev().append(response);
}
},
error : function(http,status,error){
$("#comment").val("Post");
$('.response').html("<span class='error'>Something went wrong</span>");
$(".response").slideDown();
}
})
})

access json object returned from php file via ajax

$(function() {
var sineData;
$.ajax({
url : '../_php/loadFromDB.php',
type : 'POST',
data : 'getSines',
dataType : 'json',
success : function (result) {
console.log(result);
sineData=result;
},
error : function () {
alert("error");
}
})
});
Under Console->All->Response in firebug I get the below as expected:
[{"userID":"1","email":"user#mail.com","number":"800.256.6547","ext":"5788","startDay":"Sunday","endDay":"Thursday"}]
but when I look at sineData it is Undefined.
I want to be able to access these values like sineData[0].email
Where am I going wrong?
async: false, ended up fixing this for me but probably isnt the best solution for most aplications.
You are probably accessing sineData outside the ajax call. You are trying to access it before the asynchronous call is done. Try this:
function whenIsDone(result){
// Do whatever you want with the variable result
console.log(result[0].email);
}
$.ajax({
url : '../_php/loadFromDB.php',
type : 'POST',
data : 'getSines',
dataType : 'json',
success : whenIsDone,
error : function () {
alert("error");
}
})

is this ajax call for below rest Service is correct or not?

Sample rest Service is below:
#RequestMapping(value = "/image/upload", method = RequestMethod.POST)
public void uploadImage(#RequestParam("image") MultipartFile fileObj)
throws Exception
{
System.out.print("File Name:"+fileObj.getOriginalFileName());
}
and i wrote ajax code like this :
and my accept application format is Json when i call this i get 400 error
$('#user_click').click(function(){
var data = {
image:$("#file_1").val
};
$.ajax({
url : "http://localhost:8080/MyProject/image/upload",
type : "POST",
contentType : false,
crossDomain : true,
data : JSON.stringify(data),
dataType : 'json',
async : true,
success : function(result) {
alert('The Selected Items uploaded');
},
error: function(message){
alert("Error:"+JSON.stringify(message));
}
});
is this ajax code is correct or not?
No, it will not work since ajax request will not transfer file data.
The solutions are
Use a file upload plugin like jquery-form
Ex:
$('#myForm').ajaxSubmit({
url : 'http://localhost:8080/MyProject/image/upload',
type : "POST",
dataType : "json",
success : function(response) {
},
error : function(response) {
}
});
Use html5 FormData (Sadly no IE support)

Resources