I'm trying to call a php script with a get request and using the data theaddress however the results are showing me the source of the page im calling.
The page im calling is here
Here is my ajax function that will get this page
$( document ).ready(function() {
var address = document.getElementById("address");
$.ajax({
url: '/r10database/checkSystem/ManorWPG.php',
type: 'GET',
data: 'theaddress='+address.value,
cache: false,
success: function(output)
{
alert('success, server says '+output);
}, error: function()
{
alert('Something went wrong, saving system failed');
}
});
});
$( document ).ready(function() {
var address = document.getElementById("address");
$.ajax({
url: '/r10database/checkSystem/ManorWPG.php',
type: 'GET',
data: 'theaddress='+address.value,
cache: false,
success: function(output)
{
alert('success, server says '+output);
}, error: function(error)
{
alert (error); // this is the change from the question
}
});
});
Put the dataType as json with a curly brace
data: {theaddress:address.value},
dataType:'json',
success: function(output)
{
alert('success, server says '+output);
}, error: function(xhr)
{
alert (xhr.status);
}
and get the data in ManorWPG.php as $_GET['theaddress']
** share the xhr.status if failed.
Related
I am creating a slim project using CRUD. I am trying to get 1 user, by using GET. When I use .append it returns data like this: the name is the request getting data from the database.
undefined
name
undefined
name
Obviously I only want one user to be shown, however when I add .html instead of .append it just comes back undefined.
This is my code that is coming back undefined
$(document).ready(function showStaff(processResponse) {
$.ajax({
type: 'GET',
dataType: "json",
url: "borrower.php/borrowers",
success: showAllBorrowers,
error: showError
});
});
//shows borrowers in HTML
function showAllBorrowers(responsedata){
$.each(responsedata.borrower,function(index,borrower){
$("#borrower_list").append("<li type='square'> Borrower Id:"+borrower.borrower_id+",FullName:"+borrower.first_name+" "+borrower.last_name+", Email:"+borrower.email);
$("#borrower_list").append("</li>");
});
}
$(document).ready(function(){
$("#btnSaveBorrower").click(function(){
var borrower=new Borrower($("#firstName_add").val(),
$("#lastName_add").val(),$("#email_add").val());
$.ajax({
type: 'POST',
dataType: "json",
url: "borrower.php/borrowers",
data:JSON.stringify(borrower),
success:function(data){
console.log(data)
location.reload();
},
});
});
});
function Borrower(firstName, lastName, email){
this.firstname=firstName;
this.lastname=lastName;
this.email=email;
}
$("#btnSearch").click(function(){
var id = $("#StaffBorrower_Id").val();
$( "#update" ).show();
$( ".delete" ).show();
$("#btnUpdateBorrower").show();
$("#btnDeleteBorrower").show();
$.ajax({
type: 'GET',
dataType: "json",
url: "borrower.php/borrowers/" + id,
success: showResponse,
error: showError
});
});
function showResponse(responsedata){
var item = $("<li>"); item.text("Borrower Id:"+responsedata.borrower_id+",FullName:" +responsedata.first_name+" "+responsedata.last_name+", Email:"+ responsedata.email);
$("#search_borrower").html(item);
item.attr("type", "square");
item.attr("data-delete-id", responsedata.borrower_id); item.attr("data-update-id", responsedata.borrower_id);
console.dir(responsedata);
}
$("#btnDeleteBorrower").click(function(){
var result = confirm("Are you sure you want to delete?");
if (result) {
var staff= delete Borrower($("#borrower_Id").val(), $("#firstName").val(), $("#lastName").val(), $("#email").val());
$.ajax({
type: 'DELETE',
dataType: "json",
url: "borrower.php/borrowers/"+$("[data-delete-id]").data("delete-id"),
success: function(res){
console.log(res);
location.reload();
},
error: showError
});
}
});
$("#btnUpdateBorrower").click(function(){
var result = confirm("Are you sure you want to update?");
if (result) {
var borrower = new Borrower ($("#firstName").val(),
$("#lastName").val(),
$("#email").val());
$.ajax({
type:'PUT',
dataType:"json",
url:"borrower.php/borrowers/"+$("[data-update-id]").data("update-id"),
data:JSON.stringify(borrower),
success:function(data){
console.log(data);
location.reload();
},
error: function(){
console.log("There seems to be an error")
}
});
}
});
The console log is working, I am getting the data in the console but it isn't being returned in the html for some reason. Does anyone have any ideas why? Thanks
I have a function which i want to execute when ajax makes a successful request but it doesn't execute here my jquery part
$.ajax({
type: "POST",
url: "https://www.example.com/create_chat.php",
data: dataString,
beforeSend: function()
{
$("#loading_indicator").show();
},
success: function(response)
{
$(".example").html(response);
}
});
here is the response from php file
<script>start_chat(); alert("testing");</script>
i tried adding this also
$(".example").find("script").each(function(i) {
eval($(this).text());
});
but nothing works
Your response from create_chat could indicate which function will be used. For example
$.ajax({
type: "POST",
url: "https://www.example.com/create_chat.php",
data: {
dataString: dataString
},
beforeSend: function()
{
$("#loading_indicator").show();
},
success: function(response) // response could be 1,2,3,4.. etc
{
if(response==1) {
start_chat();
}
if(response==2) {
stop_chat();
}
if(response==3) {
change_chat_room();
}
...
$(".example").html(response);
}
});
While there are work arounds, you should never be executing the response of an Ajax call directly.
"Scripts in the resulting document tree will not be executed,
resources referenced will not be loaded and no associated XSLT will be
applied."
http://www.w3.org/TR/XMLHttpRequest/#document-response-entity-body
Best practice is to respond with data, typically in the form of JSON, JSONP or HTML.
Try This,
$.ajax({
type: "POST",
data: {
'dataString': dataString
},
url: 'https://www.example.com/create_chat.php',
success: function(data) {
start_chat();
alert('Success');
},
error: function(data) {
alert('failed');
}
});
For some reason I can't make this code work, I am trying to reload/refresh the page using GET. All the sample codes that I have found uses POST but I can't go to that because our forms require GET.
This is my code:
$.ajax({type: "GET",
url: "CorrectResults",
data: data,
beforeSend: function() {
console.log("Submitting Data");
},
cache: false,
success: function(html) {
document.location.reload();
//location.reload(true);
//$("#acceptDiv").html(html);
}
});
It should be a simple way to get this done, why this is so easy with POST and not with GET?
Here we go:
$.ajax({type: "GET",
url: "CorrectResults",
data: data,
beforeSend: function() {
console.log("Submitting Data");
},
cache: false,
success: function(html) {
window.location.href= "../yourUrl";
}
});
Hope it helps;)
if you have not any other error in your script( please go to console in your browser press F12),
then you can try with location.reload();
in your success method, like that:
$.ajax({type: "GET",
url: "CorrectResults",
data: data,
beforeSend: function() {
console.log("Submitting Data");
},
cache: false,
success: function(html) {
location.reload();
}
});
The following is my code to send data (formatted XML) but it gives an error. Can anybody tell me what am I doing wrong?
$.ajax({
type: "POST",
url: "XMLFile1.xml",
dataType: "xml",
data: "<event><text>text</text> <date>date</date></event>",
success: function () {
alert("msg sent");
},
error: function() {
alert("error");
}
});
in my application I have to make an ajax call to php file.it works proper in all devices. but when I tried it on ipad mini it not calls the php, so that the functionality not works, I've seen so many question about this problem and edited my code like this.
jQuery.ajax({
type: "POST",
async: true,
cache: false,
url: "directory/phpfile.php",
data: data,
success: function(response) {
}
});
my old code is
jQuery.ajax({
type: "POST",
url: "wp-admin/admin-ajax.php",
data: data,
success: function(response) {
}
});
and the problem still cant resolve . so please any one tell me how to resolve this.
Please use this code
$("#ajaxform").submit(function(e)
{
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
$.ajax(
{
url : formURL,
type: "POST",
data : postData,
success:function(data, textStatus, jqXHR)
{
//data: return data from server
},
error: function(jqXHR, textStatus, errorThrown)
{
//if fails
}
});
e.preventDefault(); //STOP default action
e.unbind(); //unbind. to stop multiple form submit.
});
$("#ajaxform").submit(); //Submit the FORM
<script type='text/javascript'>
$(document).ready(function startAjax() {
$.ajax({
type: "POST",
url: "test.php",
data: "name=name&location=location",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
});