i have tried to send uploaded file to server by using ajax like this
formData.append('foldername',fname);
formData.append('file', file)
$.ajax({
url: 'imageupload',
data: formData,
processData: false,
contentType: false,
type: 'POST',
success:function(response)
{
alert(response);
alert(response.imagename);
}
});
it is send data to server successfully but i have sent response from server like this
res.writeHead(200,{'Content-Type':'text/html', 'Access-Control-Allow-Orgin':'*'});
res.write(JSON.stringify({"imagename":"1.jpeg","imageid":"xxxxxxxxxxxxxxxxxxx"}));
res.end();
i have written two alert in success function . in first alert i got like this
{"imagename":"1.jpeg","imageid":"xxxxxxxxxxxx"}
i have written second alert for to get imagename but i got undefine
so i could not get specific key value. how to resolve this?
change
res.writeHead(200,{'Content-Type':'text/html', 'Access-Control-Allow-Orgin':'*'});
to
res.writeHead(200,{'Content-Type':'application/json', 'Access-Control-Allow-Orgin':'*'});
jquery don't convert a JSON string to Javascript Object if the content type is 'Content-Type':'text/html'. So, 'Content-Type':'application/json' or 'Content-Type':'text/json' will do the magic. You will get the response as Javascript object in the client. So, you can do
alert(response.imagename);//alerts image name