I am new to PHP. I am currently working on AJAX, and in that I want a response from my file like this:
{'success':true,'id':234,'name':'X-ghfhg','type':'campaign','value':'fghfgh'}
This is in responseText. I want to use the id
of that response. I tried JSON.Parse
Method but it is not working.
Here is my AJAX:
$('#ButtonAddHeader').click(function(){
// addEmailHeader();
var HeaderName = $('#HeaderName').val();
var EmailType = $('#emailType').val();
var HeaderValue = $('#HeaderValue').val();
if(HeaderName == "X-" || HeaderValue == "" )
{
$('#addHeaderMessage').text("Please fill in all the required fields below").addClass('text-error');
}
else
{
$.ajax({
type:'POST',
dataType: 'json',
url:CONTROLLER_URL+'snippet_addEmailHeader',
data : { HeaderName : HeaderName, EmailType : EmailType, HeaderValue : HeaderValue },
complete: function(response){
// Handle the complete event
console.log(response.responseText);
},
});
});
Define a success
callback in your ajax declaration. The parameter to that callback function ( json
in this example) is then usable as a JSON object:
success: function(json) { // You're looking for: json.id }
try like this
$.ajax({
type:'POST',
dataType: 'json',
url:CONTROLLER_URL+'snippet_addEmailHeader',
data : { HeaderName : HeaderName, EmailType : EmailType, HeaderValue : HeaderValue },
success: function(data) {
var mydata = JSON.parse(data);
console.log(mydata['id']);
}
suppose your json like-
info[{'success':true,'id':234,'name':'X-ghfhg','type':'campaign','value':'fghfgh'}];
then,
jQuery.ajax({
type:"POST",
url: CONTROLLER_URL+'snippet_addEmailHeader',
success: function(data) {
if(data)
{
var objJSON = JSON.parse(data);
var classinfo = objJSON.info[0];
var id = val(classinfo.id);
alert(id);
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.