AJAX .html(response) not loading response - ajax

Been looking at this for about two hours - I can't figure out why it's not working as $('#add').html(response), but works fine when I change the function to $('.add').html(respoonse).
Any help is greatly appreciated...
function addc() {
id=$('#id').val();
$.ajax ({
method: "GET",
url: 'addcal.php',
data: {"case":id},
success: function(response) {
$('#add').html(response);
}
});
}

It would help to see the HTML for the element you're trying to update. But also, the "#" symbol will search for an item by id. The "." symbol is to find an element by class.

Related

get data from ajax as an attribute value for callback function

Im new to ajax. I was trying to find the answer but was not lucky to find the corresponsing one. Basically I need to use an ajax to get some data and after that to put this data to the variable that later will be used as an attribute for the callback function with custom code.
This ajax part is just a method of myObject.
So, in the end I need this kind of functionality:
myObject.getData(url, callback(data) {
//my custom code of what I wanna do after ajax is complete
});
My code
/*
HERE COME SOME PROPERTIES AND OTHER METHODS WICH IS NOT THE CASE
*/
//This is where Im stuck
var getData = function getFromUrl($url) {
$.ajax({
type: 'get',
url: $url,
dataType: 'html',
success: function(html) {
$obj = html;//Im lost on this step!
},
});
};
P.S. Im trying to find an async way (without using async:false). Hope its possible
First I encountered many problems. My first problem was No Access-Control-Allow-Origin, most websites dont allow you to just scrap get their data for security reasons. Luckily someone already made a proxy: http://cors.io/ . Second problem is that you cant embed http on https, so I cant use jsfiddle to show you this working, it works on my local enviroment. After you get the raw html you have to parse it, you can do it with full regex, or you can power yourself with jquery like I'm doing on this example. What we're doing is checking stackoverflow.com and getting the amount of featured questions with .find(".bounty-indicator-tab").first().html(); But once you have the full html you can get any data you need.
var getData = function getFromUrl(url) {
$.ajax({
url: 'http://cors.io/?' + url,
crossDomain: true,
dataType: 'html',
success: function (html) {
var match = $(html).find(".bounty-indicator-tab").first().html();
console.log(match);
return match;
},
error: function(e) {
console.log('Error: '+e);
}
});
};
url = 'http://stackoverflow.com/';
data = getData(url);
//You cant use data yet because its working async

ajax respsonse undefined object

Although there is many posts with similar question,i couldnt managed to resolve it.
I can see at Console of my browser my object returns ok as array.
when i alert data.AlliasName i get undefined.
my json returns
{"data": [{"id":1,"FirstName":"101","LastName":"101","AlliasName":"101","Address":"kentro","Type":"1"}]}
$.ajax({
url: urlreq,
dataType: "json",
success: handleData
});
function handleData(data) {
alert(data.AlliasName);
console.log(data);
//do some stuff
}
I ve tried also puting the alert into success, data[0].AlliasName and eval func,always same result.
The data being returned contains a property called "data", which is an array, so the way to access this would be:
function handleData(data) {
console.log(data.data[0].AlliasName);
}

AJAX for searching posts wordpress

this is returning errors that occur when you try to load an url with [] in it(as far as I know)... I dont know why i am getting this errors... please help me correct the code below:
Errors:
Warning: strpos() expects parameter 1 to be string, array given in C:...\query.php on line 1718 <<<
Warning: preg_split() expects parameter 2 to be string, array given in C:...\query.php on line 1719 <<<
Warning: Invalid argument supplied for foreach() in C:...\query.php on line 1720 <<<
$("#submit").live("click", function() {
$("#form").submit(function() {
event.preventDefault();
var teste=$(this).serialize();
$.ajax({
type: "POST",
url: "./.../search.php",
data: teste,
success:function(data) {
$("#index_content").html(data);
}
});
});
});
Edited version that works :)
$("#submit").live("click", function() {
$("#form").submit(function() {
event.preventDefault();
teste = $('#form').serialize();
$.ajax({
type: "POST",
url: "./.../search.php",
data: { 'album-features': teste },
success:function(response) {
$("#index_content").html(response);
}
});
});
});
Since you are using wordpress, why not to use its built in ajax and send the request to admin-ajax.php. I see that you are sending it to search.php, which is not a good approach especially for security.
Write your php code to be executed in functions.php and add actions.
This page is containing in its answer the detailed way to do so: Dynamically changing navigation links (next and previous) in Wordpress via AJAX
good luck, I am here to assist you

Jquery AJAX for fetching JSON, affected by URL prefix (www vs. no www)?

I have come across a peculiar item in JQuery that I am hoping somebody can help me to understand.
I've spent much of the day trying to get JQUERY's AJAX 'success' function to be raised when returning JSON from the server.
I checked the JSON # JSONLint to ensure validity, checked encoding, tried different headers, but still PROBLEMS.
After a couple hours, I switched the url (by accident!)
from
http//www.testing.com/_r4444/myfile.php
to the exact same thing WITHOUT the www... and it suddenly worked.
I have no clue why this would be the case - any ideas?
the snippet follows
$(document).ready(function() {
$.ajax( {
type: "POST",
contentType: "application/json",
url: "http://testing.com/_r4444/getter.php",
beforeSend: function(x) {
if(x && x.overrideMimeType) x.overrideMimeType("application/json;charset=UTF-8");
},
data: "pass=TEST",
dataType: "json",
error: function (xhr, status) {
alert(status);
},
success: function (result) {
alert(result);
}
});
});
Are you using "www" on the page in the browser?
Try switching the call to not include the domain, like:
"/_r4444/getter.php" instead of the full domain.

Umbraco - load content with Ajax

I'm new to Umbraco and only started to figure out the ins and outs of it.
Anyway, I've figured out on my own the way document types, macros, templates, xslt files work and am now trying to do some other stuff. Namely I need to load a document content using an AJAX call. It's basically a panel with a menu (dynamic, which I figured out how to load) that loads content depending on the menu item selected (the documents loaded with the menu). What I need to figure out is how to get that content using an AJAX call since I don't want to reload the page.
Is this done using Umbraco BASE extensions or am I off in my thinking here? If so, how exactly? Do I just write a class and then stitch together an HTML string in a method?
Thanks for the help
You can use rest methods. For this you have to edit restExtensions.config on the config folder.
Ajax Call
$.ajax({
type: 'POST',
url: "/base/AliasName/GetData.aspx",
data: {
},
success:
function (data) {
}
});
restExtensions.config
<ext assembly="/DllName" type="Namespace.ClassName" alias="AliasName">
<permission method="GetData" returnXml="false" allowAll="true" />
</ext>
Yup this is exactly the scenario that Base is used for.
You can find documentation on using base here:
http://our.umbraco.org/wiki/reference/umbraco-base/simple-base-samples
For the consumption of base via AJAX then JQuery is the answer.
http://api.jquery.com/jQuery.ajax/
Here's a hacked together example (not tested code):
$(document).ready(function ()
{
$(".buttonListener").click(function ()
{
$.ajax(
{
url: '/Base/TestAlias/Hello.aspx',
success: function (data, textStatus, XMLHttpRequest)
{
alert(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown)
{
alert("Ka pow!");
}
});
return true;
});
Ajax call using umbraco in MVC
$('#TestClick').on('click',function(){
$.ajax({
url: 'umbraco/surface/Home/TestPage',
type: 'POST',
data: { id:10001},
success: function (data) {
alert(data);
},
error: function () {
alert("error");
}
});
})

Resources