Getting response back from ajax php - ajax

Trying to initialize sessions to transfer data to another page This is the start and I am not getting response back from PHP.
<code>
<script>
function lode(value){
x = value;
alert(x);
$.ajax({
method: "POST",
url: 'file.php',
data: { x: x},
success: function(response){ alert(response);
document.getElementById("col1").innerHTML=response;
},
error: function(xhr, status, error){
console.error(xhr);
}
})
}
</script>
<?php
if (!empty($_POST["x"])) {
echo "Variable 'x' is set.";
exit;}
?>
</code>

Related

Why i can not fetch data from controller from ajax function?

I'm beginner in asp.net mvc ,and want to fetch simple json from controller to ajax variable,for that purpose in view page write this ajax function:
<script>
var OutPut;
OutPut = "behzad";
function CallService() {
$.ajax({
url: '#Url.Action("callService", "myPassword")',
type: 'GET',
dataType: 'json',
cache: false,
data: { 'id': 2 },
success: function (color) {
OutPut= color;
},
error: function () {
alert('Error occured');
}
});
alert("I think is ok!"+":"+OutPut);
}
</script>
and this controller:
[HttpGet]
public JsonResult callService(int id)
{
string JSON = "behzad";
return Json(JSON,JsonRequestBehavior.AllowGet);
}
that ajax function call with this html code in view page:
<button type="button" class="btn btn-success" onclick="CallService()">Success</button>
but this line in ajax function:
alert("I think is ok!"+":"+OutPut);
Output is undefined,what happen?is controller return null?or why i get undefined alert?thanks.
Since the AJAX call is asynchronous, you should place the alert inside the success callback:
<script>
function CallService() {
$.ajax({
url: '#Url.Action("callService", "myPassword")',
type: 'GET',
cache: false,
data: { 'id': 2 },
success: function (color) {
alert("I think is ok!" + ":" + color);
},
error: function () {
alert('Error occurred');
}
});
}
</script>

Cannot pass data ajax in ci

$("body").on('click', '.name', function(e) {
//var valueofbutton = $(this).val();
$.ajax({
type: "POST",
url: "response",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
});
my response controller
Class Response extends CI_Controller{
public function index()
{
$data=$this->input->post('name');
echo $data;
}
}
it shows me some errors i dont know what kind of error is it !
my alert gives this information
Data Saved: <br />
<b>Warning</b>: Unterminated comment starting line 25 in <b>C:\xampp\htdocs\vacationgod\application\controllers\response.php</b> on line <b>25</b><br />
John
and i cant see any print information like john which i pass to response controller
Just change your ajax code.Controller is ok.
$("body").on('click', function(e) {
$.ajax({
type: "POST",
url: '<?=base_url("controller_name/function_name") ?>',
data: {name: "John",location:"Boston"},
success: function(response) {
alert("Data Saved: " + response);
}
});
});
Your data should be an object and remove .name. change that line in you ajax like this:
$("body").on('click', function(e) {
$.ajax({
type: "POST",
url: "response.php",
data: {name: "John",location:"Boston"},
success: function(msg) {
alert("Data Saved: " + msg);
}
});
});

ajax form submits fine, but success does not work?

I have a simple form that submits with jquery and ajax. the form submits fine and the data posts, but the success message will not call... just the error message. can someone tell me what I am doing wrong.
<script type="text/javascript" >
$(function() {
$(".form-submit").click(function() {
var esn = $("#esn").val();
var dataString = 'esn='+esn;
if(esn=='')
{
$('.error').show().fadeOut(3000);
}
else
{
$.ajax({
type: "post",
url: "include/submit_repair_form.php",
data: dataString,
success: function(result) {
alert(result);
$('.testing').hide();
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.statusText);
}
});
}
return false;
});
});
</script>
You forgot to set the result variable in function()
Like this:
success: function(result) {
alert(result);
$('.testing').hide();
}

Jquery Annotate / PHP error by sending the object value

Send var annotation_id:
echo $json = json_encode(array('annotation_id'=>$save[0]));
Get var annotation_id:
if (image.useAjax) {
$.ajax({
url: image.saveUrl,
data: form.serialize(),
error: function(e) {
alert("An error occured saving that note.");
},
success: function(data) {
if (data.annotation_id != undefined) {
editable.note.id = data.annotation_id;
alert(editable.note.id);
}
},
dataType: "json"
});
}
So...what's wrong with my code ?
Not getting the value of this variable.

AJAX Internal server error

I couldnt find out what is the error.
<script type="text/javascript">
$(document).ready(function () {
$("#btnsumbit").click(function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
data: '{"username":"' + $("input#txtuser").val() + '","password":"' + $("input#txtpwd").val() + '"}',
url: 'http://localhost:53179/hdfcmobile/WebService.asmx/Login_Data',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success:
function (data, textStatus, XMLHttpRequest) {
var status = data.Status;
alert(data.d);
},
error:
function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
});
});
</script>
I am getting 500 internal server error.How to call this webservice.I have passes the method with the url.Thanks for any help...
First thing, the way you are sending is wrong, send it like this
data: {
"username": $("input#txtuser").val(),
"password": $("input#txtpwd").val()
}
Next make sure, url: http://localhost:53179/hdfcmobile/WebService.asmx/Login_Data is returning JSON output.

Resources