request data to ajax call is converting to js object - javascript

Here is My ajax call
$.ajax({
url:'http://localhost:8081/organizations/',
data: JSON.stringify({"name":"karthik"}),
type: 'POST',
dataType :'json',
processData : true,
success: (function(response){ alert("response "+response);}),
error: (function(err){ console.log(err); })
});
In the api, when i tried to print request.data, why i am getting like this {u'{"name":"karthik"}': [u'']}

Replace below line
success: (function(response){ alert("response "+response);}),
by newone
success: (function(response){ alert(JSON.stringify(response));}),
In your code and then try it will work.

Related

Returns error when consuming this php response

Return error in the query
From the browser the answer is correct.
$.ajax({
type: "POST",
url: url,
async: true,
contentType: " charset=utf-8",
dataType: "XMLHttpRequest",
success: function (response) {
console.log(response);
},
error: function (msg) {
console.log(msg);
}
});
The message says "error".
I see three issues. First, dataType is a choice of xml, json, script, or html, unless you did something really fancy. jQuery can guess it based on received data though, so there is normally no need to set it. But if you want to be explicit (assuming your page returns json):
dataType: "json"
Second, contentType value looks like some truncated thing. I would just completely remove it, as you are not sending any data and just requesting a page.
Finally, when you are sending no data and just requesting a resource, the best is to use GET.
All in all:
$.ajax({
type: "GET",
url: url,
async: true,
dataType: "html",
success: function (response) {
console.log(response);
},
error: function (msg) {
console.log(msg);
}
});

How to get each data before ajaxSend in JSON not after complete/success

I want all the ajax params before sending an ajax request in JSON format and I need to encrypt each value in JSON and again pass to the ajax request.
I get data in URI format as see in below code, not in JSON. How can I get that?
Around 200 Ajax in this format:
$.ajax({
type: "POST",
url: site_url + "user/user/login_action",
data: login_parms,
success: function (data) {
},
error: function (xhr, textStatus, errorThrown) {
}
});
Before Ajax Call:
$(document).ajaxSend(function(event, jqxhr, settings) {
console.log("settings :",settings.data);
});
Console log:
settings : vEmail=disha.c1%40grr.la&vPassword=123456789
Also if in AJAX use formData then how we can get each value of form data?
If you want to send a AJAX JSON CALL you must to use:
$.ajax({
type: "POST",
url: site_url + "user/user/login_action",
dataType: "json",
async: false,
contentType: "application/json",
data: JSON.stringify(login_parms),
success: function (data) {
},
error: function (xhr, textStatus, errorThrown) {
}
});
if you want to modify the param:
$.ajax({
beforeSend: function(xhr){
this.data
}
});

send array to post ajax

i need send mi post in this format
{
"elements" : ["elem1", "elem2","elem3" ]
}
and i have created this array :
var miElem=["elem1", "elem2","elem3" ]
$.post({
url: 'url',
data: {"elements" : miElem} ,
dataType: 'json',
success: function(data) {
console.log(data)
}.bind(this),
error: function(xhr, status, err) {
console.log(err)
}.bind(this)
});
but dont work, the data send well, but the format is incorrect
Use $.ajax() like this.The use of $.post is not correct.Check the syntax for $.post.here https://api.jquery.com/jquery.post/
var miElem=["elem1", "elem2","elem3"];
$.ajax({
url: 'url',//give your url here
type:'POST',
data: {elements : miElem} ,
dataType: 'json',
success: function(data) {
alert('success');//test for successful ajax request
console.log(data);
},
error: function(xhr, status, err) {
alert('error');//test for unsuccessful ajax request
console.log(err);
}
});
You should used $.ajax instead of $.post.
In $.ajax don't use success and error parameters. Use .done(), .fail(), .. (http://api.jquery.com/jquery.ajax/) it's more comprehensive. You can use .then() if you want to use promise.
Datatype parameter is for response type.
Your request is failing because
you passed incorrect arguments to $.post() method.
you did not set type:'POST' if you intended to use $.ajax.
for $.post() do the following:
$.post('url', {elements: miElem}).done(function(data) {
console.log(data);
}).fail(function (error)
console.error(error);
});
for $.ajax do the following:
$.ajax({
type: 'POST',
url: 'url',
data: {elements: miElem},
success: function (data) {
console.log(data);
},
dataType: 'json'
});

How to get the PHP var after Jquery $.ajax

I use $.ajax to send some data to testajax.phpenter code here where data are processing. Result of this proces is link. How to get access to this? I think about sessions or cookies. Set in session $test and get access in another files.
$.ajax({
url: 'testajax.php',
type: 'post',
dataType: 'json',
success: console.log('success');
data: { json : jsonObj }
});
In testajax.php I have someting like this
echo "PDF: <a href=images/test.pdf>$dir.pdf</a>";
$test = "PDF: <a href=images/test.pdf>$dir.pdf</a>";
How to get the $test or output the echo after call $.ajax
You can use success' function to get request from PHP.
$.ajax({
url: 'testajax.php',
type: 'post',
dataType: 'html',
data: { json : jsonObj }
success: function(data) {
var request = data;
console.log(data);
$('#link_container').html(data);
}
});
Your console now holds PDF: <a href=images/test.pdf>($dir content).pdf</a>.
Also the variable request holds the same result that you can use anywhere in the script.
Result is appended to #link_container.
Use the success() method for display the result.
$.ajax({
url: 'testajax.php',
type: 'post',
dataType: 'json',
success: function (data){ // <= define handler for manupulate the response
$('#result').html(data);
}
data: { json : jsonObj }
});
Use below code It may help you.
$.ajax({
url: 'testajax.php',
type: 'post',
dataType: 'json',
data: { json : jsonObj },
onSuccess: successFunc,
onFailure: failureFunc
});
function successFunc(response){
alert(response);
}
function failureFunc(response){
alert("Call is failed" );
alert(response);
}

Reading values from ajax return in json format

In ajax return, i am getting json as
[{"colourname":"red,yellow"}]
i want to fetch "red,yellow" string from json ,
ajax call se ,
$.ajax({
type: "POST",
url: "loadData.php",
data: {
productid: 'getId'
}
}).done(function (msg) {
alert('get ' + msg);
});
I tried ,
msg[0].colourname
msg["colourname"]
Nothing worked how can i access values?
The response returned by $.ajax in done is a raw string, not a JavaScript object. Set dataType: 'json' in the ajax configuration and jQuery will parse the JSON msg as a JavaScript object.
$.ajax({
type : "POST",
url : "loadData.php",
data : {
productid : 'getId'
},
dataType: 'json',
}).done(function(msg) {
alert('get '+msg);
});
Setting the dataType explicitly is not required if you send the server response with Content-Type: application/json
BTW, you should use an array for colourNames: {"colournames":["red","yellow"] }
try this
$.ajax({
type: "POST",
url: "loadData.php",
dataType: 'json'
data: {
productid: 'getId'
}
}).done(function (msg) {
alert('get ' + msg);
});
});

Categories

Resources