Send large Json data to php via ajax call - php

I need to post large amount of json data to php file via ajax, but its is showing me 413 entity too large error.
I have tried using data type as json but it still shows the same error.
$.ajax({
url: ajaxURL,
data: "ajax=true&action=manageSavedLayouts&a=" + result + "&b=" + encodeURIComponent(productdb),
success: function(result) {
console.log(result);
// alert(result);
}
});
This is the code i am using to make the ajax call and the variable which is causing problem is productdb.
Any help will be greatly appreciated.
Thanks in advance.

$.ajax({
url: ajaxURL,
type: "POST",
data: {
ajax: true,
action: "manageSavedLayouts",
a: result,
b: productdb
}
success: function(result) {
console.log(result);
}
});

Use type POST, e.g.:
$.ajax({
url: ajaxURL,
type: 'POST',
data: "ajax=true&action=manageSavedLayouts&a=" + result + "&b=" + encodeURIComponent(productdb),
success: function(result) {
console.log(result);
// alert(result);
}
});

Related

access the json encoded object returned by php in jquery

I want to post some data to php function by ajax, then get the encoded json object that the php function will return, then I want to get the information (keys and values) from this object, but I don't know how, here is my code:
$.ajax({
url: "functions.php",
dataType: "JSON",
data: {id: id},
type: 'POST',
success: function(json){
for(var i=0;i<json.length;i++){
alert(json['fname']);
}
}
});
and here is the json object returned:
[{"id":"1","fname":"kjhkj","mname":"kjhjh","lname":"lname","prefix":"Mr.","suffix":"jhkjhk","email":"hf#dd.com","image":"11281454_423648214427141_318277024_o.jpg","info":"hjgvhd"}]
Try:
$.ajax({
url: "functions.php",
dataType: "JSON",
data: {id: id},
type: 'POST',
success: function(json){
for(var i=0;i<json.length;i++){
alert(json[i].fname);
}
}
});
It is rather simple to do this:
var data = jQuery.parseJSON(json);
jQuery.each(data, function(i, item) {
jQuery('.derp').append(item.mname + "<br />");
});
Example
Reference
jQuery.each()
jQuery.parseJSON()

Knockout and AJAX post request PHP

I am attempting to get our knockout form to submit to a php script and am getting undefinedIndex errors. I am pretty sure it is the way we are sending the data over in our ajax function.
Here is the ajax:
$.ajax({
url: '/orders/add',
type: 'post',
data: {payload:ko.toJSON(allModel)},
contentType: 'application/json',
success: function (result) {
alert(result);
}
});
Here is the PHP (we use laravel)
return json_decode($_POST["payload"]);
Pete is correct. You need to use just one data field. If you want a variable, define it before the $.ajax post
var dataPayload = ko.toJSON(allModel);
$.ajax({
url: '/orders/add',
type: 'post',
data: {payload: dataPayload},
contentType: 'application/json',
success: function (result) {
alert(result);
}
});

Ajax passing data to php script

I am trying to send data to my PHP script to handle some stuff and generate some items.
$.ajax({
type: "POST",
url: "test.php",
data: "album="+ this.title,
success: function(response) {
content.html(response);
}
});
In my PHP file I try to retrieve the album name. Though when I validate it, I created an alert to show what the albumname is I get nothing, I try to get the album name by $albumname = $_GET['album'];
Though it will say undefined :/
You are sending a POST AJAX request so use $albumname = $_POST['album']; on your server to fetch the value. Also I would recommend you writing the request like this in order to ensure proper encoding:
$.ajax({
type: 'POST',
url: 'test.php',
data: { album: this.title },
success: function(response) {
content.html(response);
}
});
or in its shorter form:
$.post('test.php', { album: this.title }, function() {
content.html(response);
});
and if you wanted to use a GET request:
$.ajax({
type: 'GET',
url: 'test.php',
data: { album: this.title },
success: function(response) {
content.html(response);
}
});
or in its shorter form:
$.get('test.php', { album: this.title }, function() {
content.html(response);
});
and now on your server you wil be able to use $albumname = $_GET['album'];. Be careful though with AJAX GET requests as they might be cached by some browsers. To avoid caching them you could set the cache: false setting.
Try sending the data like this:
var data = {};
data.album = this.title;
Then you can access it like
$_POST['album']
Notice not a 'GET'
You can also use bellow code for pass data using ajax.
var dataString = "album" + title;
$.ajax({
type: 'POST',
url: 'test.php',
data: dataString,
success: function(response) {
content.html(response);
}
});
$.ajax({
type: 'POST',
url: 'test.php',
data: { album: this.title },
success: function(response) {
content.html(response);
}
});

jQuery Ajax post to php not catching variable

What am i doing wrong. PHP doesn't seem to catch title and wrapper from $.ajax. Does the code look correct. The success message i get indicate an error that title is not found.
jQuery main.html
$.ajax({
type: "POST",
url: "process.php",
data: 'title=test&wrapper=testing',
success: function(msg){
alert( "Data Saved: " + msg );
}
});
PHP process.php
<?php
$title = $_REQUEST['title'];
$wrapper = $_REQUEST['wrapper'];
...
?>
Take a look: jQuery.ajax()
The data parameter is better to be a Key/Value pairs object, it's cleaner and easier to debug :)
$.ajax({
type: "POST",
url: "process.php",
data: {
title: 'test',
wrapper: 'testing'
},
success: function(msg){
alert( "Data Saved: " + msg );
}
});
Thats a good solution.but if I try to send data through a form in a webservice.
$.ajax({
type: "POST",
url: "process.php",
data: {
title: $('#title').val,
name: $('#name').val
},
success: function(data){
alert(data );
}
});
Here title and name are forms element in client side.but i am not able to get post value in json based webservice file say process.php

Try to get the right GET link which shows the good info in shoppingcart

Ok here is my problem after months of searching over the internet. I want to get the right link from jquery to take the right div and show them on the page. For now the files takes the root with GET
I got 2 files.
shopping_cart.php and jquery-oscart.js
jquery-oscart.js
$.ajax({
type: 'POST',
url: encodeURI($(location).attr('href')) + '&action=update_product&ajax=1',
data: $('form[name=cart_quantity]').serialize(),
async:false,
success: function(data) {
$("#content-body").html(data);
//Hide_Load();
//update_cart();
},
dataType: 'html'
});
// Updating cart total
//
$.ajax({
type: 'POST',
url: encodeURI($(location).attr('href')) + '&action=update_product&show_total=1&ajax=1',
data: $('form').serialize(),
success: function(data) {
$('#boxcart-total').html(data);
//Hide_Load();
}
});
return(false);
});
The action: .attr('action')
The div that show up should be #content_body from the shoppingcart file.
In the shoppingcart.php file there is an action that calls:
<script type="text/javascript" src="js/jquery-oscart.js"></script>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
In normal state there is no problem.
here is my question.
When execute the files Firefox gives me the following rules:
POST domain../index.php?option=com_oscommerce&osMod=shopping_cart&Itemid=2&action=update_product&ajax=1
GET domain../index.php?option=com_oscommerce&osMod=shopping_cart&Itemid=2
POST domain../index.php?option=com_oscommerce&osMod=shopping_cart&Itemid=2&action=update_product&show_total=1&ajax=1
GET domain../index.php?option=com_oscommerce&osMod=shopping_cart&Itemid=2
instead of
POST domain../index.php?option=com_oscommerce&osMod=shopping_cart&Itemid=2&action=update_product&ajax=1
GET domain../index.php?option=com_oscommerce&osMod=shopping_cart&Itemid=2&ajax=1
POST domain../index.php?option=com_oscommerce&osMod=shopping_cart&Itemid=2&action=update_product&show_total=1&ajax=1
GET domain../index.php?option=com_oscommerce&osMod=shopping_cart&Itemid=2&show_total=1&ajax=1
In the GET. I miss &ajax=1 and &show_total=1&ajax=1
Something had to be changed in the jquery_oscart.js but I don't know where to change...
I tried the .load function with the right link but that's not a solution.
I hope someone can help me with this.
The original code is:
jQuery.ajax({
type: 'POST',
url: encodeURI($('form[name=cart_quantity]').attr('action')) + '&ajax=1',
data: jQuery('form[name=cart_quantity]').serialize(),
success: function(data) {
jQuery("#content-body").html(data);
//Hide_Load();
//update_cart();
}
});
// Updating cart total
jQuery.ajax({
type: 'POST',
url: encodeURI($('form[name=cart_quantity]').attr('action')) + '&show_total=1&ajax=1',
data: jQuery('form').serialize(),
success: function(data) {
jQuery('#boxcart-total').html(data);
//Hide_Load();
}
});
return(false);
});
it gives the link:
domain/index.php&ajax=1
instead of
domain../index.php?option=com_oscommerce&osMod=shopping_cart&Itemid=2&action=update_product&ajax=1
Could it be something with the 'form'? It seems it send me to index.php instead of index.php?option=com_oscommerce&osMod=shopping_cart
Problem solved the ? option... link was hidden. With another file I get is showed. Now the POST links are fine. The problem I only got is the GET link.
It seems url: encodeURI($('form[name=cart_quantity]').attr('action')) + '&ajax=1',
Gets 2 links a POST en ea returning GET. The returning link missed the &ajax=1 at the end.
Maybe try
url: encodeURI($(location).attr('href') + '&action=update_product&ajax=1'),
(I put &action=blahblah inside encodeURI)
Problem could be solved by adding + 'format=ajax' in the url
like:
jQuery.ajax({
type: 'POST',
url: encodeURI($('form[name=cart_quantity]').attr('action')) + '&format=ajax'+ '&ajax=1',
data: jQuery('form[name=cart_quantity]').serialize(),
success: function(data) {
jQuery("#content-body").html(data);
//Hide_Load();
//update_cart();
}
});
// Updating cart total
jQuery.ajax({
type: 'POST',
url: encodeURI($('form[name=cart_quantity]').attr('action')) + '&format=ajax'+ '&show_total=1&ajax=1',
data: jQuery('form').serialize(),
success: function(data) {
jQuery('#boxcart-total').html(data);
//Hide_Load();
}
});

Categories