I have tried this below link but not working in my server.
Jquery Ajax - post huge string value
Try this one
$.ajax({
type: "POST",
url: "your_url",
data: {string:3MB_string},
contentType: "application/json",
dataType: "json",
success: function (data) {
if (data == undefined) {
alert("Error : 219");
}
else {
alert(data.d);
}
},
error: function (data) {
if (data == undefined) {
alert("Error : 465");
}
else {
alert("Error : 468 " + data.d);
}
}
});
Related
I want to get the value from $data->username but it returns NULL because input is not json. What am I doing wrong here?
Here is my current code.
JS:
$(function() {
$("#login-form").submit(function(e) {
e.preventDefault();
$.ajax({
url: 'http://penguin.linux.test/api/login.php',
type: 'post',
dataType: 'application/json',
data: $("#login-form").serialize(),
success: function(data) {
console.log(data);
},
error: function(data) {
console.log(data);
}
});
});
PHP:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$data = json_decode(file_get_contents("php://input", true));
}
I'm learning PHP and JS, and while doing a Get with Ajax, the PH return is coming in white. I'm turning the net but not finding anything, I'd like some help.
$idEvent = $_REQUEST["idEvent"];
$event = $this->model->getById($idEvent);
header('Content-type: application/json');
echo json_encode($event, JSON_PRETTY_PRINT);
exit;
$('#ShowEvent').click(function() {
var idEvent = document.getElementById("ShowEvent").getAttribute("idEvent");
$.ajax({
url: 'http://localhost/salgadar/public_html/Event/GetEventById',
type: 'get',
data: {
idEvent: idEvent
},
dataType: 'JSON',
success: function(data) {
alert('AJAX call was successful!');
$("#AjaxReturn").html(data)
},
error: function() {
alert('There was some error performing the AJAX call!');
}
});
console.log(data);
});
ReferenceError: data is not defined
First place the console.log() inside your ajax call, like this:
$.ajax({
url: 'http://localhost/salgadar/public_html/Event/GetEventById',
type: 'get',
data: {
idEvent: idEvent
},
dataType: 'JSON',
success: function(data) {
alert('AJAX call was successful!');
console.log(data); //HERE CODE
$("#AjaxReturn").html(data);
},
error: function() {
alert('There was some error performing the AJAX call!');
}
});
Then tell me what appears and we will continue to solve the problems.
I am using ajax request to a php script in order to registrate a new user, it works and saves it in the database, but returns as error and not as success.
This is my ajax request:
$.ajax({
type: "POST",
url: "http://localhost/webAPI/register.php",
data: dataString,
crossDomain: true,
cache: false,
success: function (data) {
if (data == 1)
alert("success");
else(data == 0 )
alert("error");
},
error: function (){
alert("An Error Ocurred");
}
});
and this is my php script:
if($_POST)
{
$user_name = $_POST['username'];
$user_password = $_POST['password'];
$joining_date = date('Y-m-d H:i:s');
//password_hash see : http://www.php.net/manual/en/function.password-hash.php
$password = password_hash( $user_password, PASSWORD_BCRYPT, array('cost' => 11));
//if($count==0){
$stmt = $db_con->prepare("INSERT INTO users(username,password,joiningdate) VALUES(:uname,:pass,:jdate)");
$stmt->bindParam(":uname",$user_name);
$stmt->bindParam(":pass",$user_password);
$stmt->bindParam(":jdate",$joining_date);
if($stmt->execute())
{
echo 1;
}
else
{
echo 0;
}
}
replace else(data == 0 ) with else
$.ajax({
type: "POST",
url: "http://localhost/webAPI/register.php",
data: dataString,
crossDomain: true,
cache: false,
success: function (data) {
if (data == 1)
alert("success");
else
alert("error");
},
error: function (){
alert("An Error Ocurred");
}
});
and in PHP code
if($stmt->execute())
{
exit(1);
}
else
{
exit(0);
}
You could parse the PHP response in JSON in order to check the result from JS.
The PHP response :
if ($stmt->execute()) {
die(json_encode(['return' => true]));
} else {
die(json_encode(['return' => false]));
}
And from JS, just check the return flag :
$.ajax({
type: "POST",
url: "http://localhost/webAPI/register.php",
data: dataString,
dataType: 'JSON', // tell JS that the PHP response is json formated
crossDomain: true,
cache: false,
success: function (data) {
if (data.return) { // check if return is true
alert("success");
} else { // if return is false
alert("error");
}
},
error: function (jqXHR, textStatus, errorThrown){
console.log(textStatus, errorThrown); // this will tell you more in case of unsuccessful request
}
});
Hope it helps.
EDIT :
Look at the Ajax error attribute function. It must tell you more about the error.
RE-EDIT :
The dataString you send to PHP must be a JSON object. So from your comment, it must look like :
var dataString = {
username: $("#username").val(),
password: $("#password").val()
};
The only problem was the missing datatype property in the ajax request.
problem when upload image MVC with ajax, i have controls_class.php content function upload_img_admin in class settings calling from page c_ajax_img.php
page c_ajax_img.php
include_once('controls_class.php');
$ajax_up_img = new settings;
$ajax_up_img->upload_img_admin(#$_FILES['file_upload']);
function upload_img_admin in class settings
function upload_img_admin()
{
$dir_name=dirname(__FILE__)."/upload/";
$path=#$_FILES['file_upload']['tmp_name'];
$name=#$_FILES['file_upload']['name'];
$size=#$_FILES['file_upload']['size'];
$type=#$_FILES['file_upload']['type'];
$error=#$_FILES['file_upload']['error'];
...
...
if( isset($_FILES['file_upload']) )
{
move_uploaded_file($path,$dir_name.$name);
...
...
echo "ok";
}
else
{
echo "File not found";
}
}
function ajax get data form and send to function previous for upload image
$(document).ready(function() {
$(".btn_upload_avatar").click(function(e) {
$('.msgerror').hide().fadeIn(1000).html( '<div class="loading"></div>');
e.preventDefault();
$.ajax({
type:"POST",
url: "../controls/c_ajax_img.php",
cache: false,
processData:false,
contentType: false,
data:$("#form_up_img").serialize(),
success: function (data)
{
if(data == 0){
$('.msgerror').addClass('msgerror_in2').html(data);
}else{
$('.msgerror').addClass('msgerror_in2').html(data);
}
}
});
});
});
Something like this might help you mate.. :)
$(document).ready(function () {
$('#UploadForm').on('submit',(function(e) {
$('.msgerror').hide().fadeIn(1000).html('<div class="loading"></div>');
e.preventDefault();
var formData = new FormData(this);
formData.append('file', input.files[0]);
$.ajax({
url: '../controls/c_ajax_img.php',
data: formData,
contentType: false,
type: 'POST',
processData: false,
success: function (data) {
console.log("success");
console.log(data);
},
error: function (data) {
console.log("error");
console.log(data);
}
});
});
});
FYI
FormData
ProcessData is set to false so that it prevents jQuery from automatically transforming the data into a query string
below is the code i am using
$(document).ready(function(){
$("form").submit(function(event) {
event.preventDefault();
var val = $("#captcha_text").val();
$.ajax({
url: 'checkAnswer.php',
type: 'POST',
dataType: 'text',
data: {
answer: val
},
complete: function(data)
{
console.log(data);
if($.trim(data) == "true")
$("form")[0].submit();
else
alert('Wrong Answer');
}
});
});
});
checkAnswer.php has this one line only which is
echo "true";
i do not know why the javascript if condition in the complete function is always going to else part and showing alert('Wrong Answer')
does any one know what could be the problem. ? In the firebug console i do see the response comming back from my ajax and response value is "true"
use:
if($.trim(data.responseText) == "true")
complete receives as 1st argument jqXHR(an extended XMLHTTP-Request-object) and not the data depending on dataType.
Or use success instead of complete
Well i think you have to do this:
$.ajax({
url: 'test.php',
type: 'POST',
dataType: 'text',
data: {
answer: val
},
complete: function(jqXHR, textStatus)
{ //it returns an jqXHR object
if($.trim(jqXHR.responseText) == "true")
$("form")[0].submit();
else
alert('Wrong Answer');
}
});
This is because jQuery returns a jqXHR object look here for reference.
EDIT You can also try this:
$.ajax({
url: 'test.php',
type: 'POST',
dataType: 'text',
data: {
answer: val
},
success: function(data)
{
if($.trim(data) == "true")
alert('ok');
else
alert('Wrong Answer');
}
});