I have such an issue
var url = "index.php?id=123&sid=321";
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&url="+url,
success: function(msg){
alert( "Data Saved: " + msg );
}
Now here we have url which contains an & , but & is used to delimit data variables to be sent to server and so since we have this sign in url it thinks that this is a delimiter, and I am not getting the full url variable.
Can somebody help with a wise solution.
Thank in advance.
Pass an object instead of string for the data field:
var url = "index.php?id=123&sid=321";
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", url: url },
success: function(msg){
alert( "Data Saved: " + msg );
}
});
var url = 'index.php?id=123&sid=321';
$.ajax({
type: "POST",
url: "some.php",
name: 'John',
url: url,
success: function(msg){
alert( "Data Saved: " + msg );
});
It would be better to pack all data with JSON as above, and build the URL server-side as part of whatever script you have handling this AJAX request.
Put your data in a JSON.
data: {id: '123', sid: '321'}
Related
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);
}
});
I have 2 ajax is an array and single char:
var jsonEncode = JSON.stringify(TableData); --> output: [{"name":"Ristha","age":"30"},{"name":"Niken","age":"25"}]
var code = $('#mutiplearray-code_reg').val(); --> output: 1RF46TA
How to send ajax post when I use 2 data like that:
$.ajax({
type: "POST",
data: "pTableData=" + jsonEncode + "code1=" + code,
success: function(msg){
// alert(msg);
},
});
When I get using in my controller:
$tableData = stripcslashes($_POST['pTableData']);
$tableData = json_decode($tableData, true);
$name1 = $tableData['name'];
$age1 = $tableData['age'];
$code1 = $_POST['code1'];
It's have error dev tool undefined code1 and pTableData?? What I'm do wrong with use multiple data in my ajax?
When I'm just using post data one of them is work correctly
Pass data as json. You passed the data as string.
$.ajax({
type: "POST",
data: {pTableData: jsonEncode, code1: code},
success: function(msg){
// alert(msg);
},
});
$.ajax({
type: "POST",
data:{'pTableData':jsonEncode,'code1':code},
success: function(msg){
// alert(msg);
},
});
$("#submit_login").click(function(){
var username=$('input[name=user_email]');
var password=$('input[name=user_password]');
var data;
data: "name="+username+"&pwd="+password,
$.ajax({
type: "POST",
dataType: "json",
url: "newExam.php",
data: data,
success: function(data) {
alert("Form submitted successfully");
}
});
});
How to give the data variable so that we can fetch it in PHP using $_REQUEST?
The above representation of the data variable shows an error.
You can pass the data as json,
$.ajax({
type: "POST",
dataType: "json",
url: "newExam.php",
data: {
name: username,
pwd: password
},
success: function(data) {
alert("Form submitted successfully");
}
});
Also, names should match the parameters in the function.
Client Side
$("#submit_login").click(function(){
var username=$("input[name='user_email']").val();
var password=$("input[name='user_password']").val();
$.ajax({
type: "POST",
dataType: "json",
url: "newExam.php",
data: {name : username, pwd : password },
success: function(data) {
alert("Form submitted successfully");
}
});
});
Server Side
<?php
// file : newExam.php
// to view post array
if(!empty($_POST))
{
print_r($_POST);
}
// access individual element
if(isset($_POST['name']))
{
echo $_POST['name'];
}
?>
The above representation of the data variable shows an error.
Absolutely. That is correct because there is an error. You have a variable and you are assigning a query string with : where it should be =:
data= "name="+username+"&pwd="+password;
But this is not a good idea because you have to post the values not the input objects. username is an html input element, instead you should post an object like:
$("#submit_login").click(function(){
var username=$('input[name=user_email]').val();
var password=$('input[name=user_password]').val();
var data = {name:username, pwd:password};
$.ajax({
type: "POST",
dataType: "json", // <---make sure you return json from the php side.
url: "newExam.php",
data: data,
success: function(data) {
alert("Form submitted successfully");
}
});
});
Your selectors dont look right to me.
var username=$('input[name="user_email"]');
var password=$('input[name="user_password"]');
Note the double quotes around the input name attributes
Can you try the below code format:
data: {name : username, pwd : password }
What is the best way to send data and receive a response dependent on that data?
Consider the PHP file used for the request:
$test = $_POST['test'];
echo json_encode($test);
I have tried unsucessfully to achieve this with:
$.ajax({
type: "POST",
dataType: "json",
data: '{test : worked}',
url: 'ajax/getDude.php',
success: function(response) {
alert(response);
}
});
Lose the quotes to pass the object:
$.ajax({
type: "POST",
dataType: "json",
data: {test : worked},
url: 'ajax/getDude.php',
success: function(data) {
alert(data);
}
});
Instead of this
data: '{test : worked}'
try
data: {"test" : worked} // Worked being your data you want to pass..
data: {"test" : "worked"} // Else enclose worked in quotes
The problem appears to be that you're submitting a string rather than a json object - change data: '{test : worked}' to data: {test : 'worked'}
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