5

this has may be discussed over several times. but just to make the idea clear. I want my function to return an object. but the way I wrote the code, it does not return the object. I tried other ways. but variable gets destroyed when it comes to outside of xhr.onload function. please help me understanding the problem

function hhtprequest(id)
{
    var pobj = function(image,name,price){
        this.image = image;
        this.name = name;
        this.price = price;
    }

    var xhr =  new XMLHttpRequest();
    xhr.open("GET","ajax/productinfforsell.php?pid="+id,true);
    xhr.onload = function(){
        if (this.readyState === 4 && this.status === 200) {
            var data = JSON.parse(this.response);

              new pobj(data.imagefile,data.name,data.price);

        }

    }

    xhr.send();
    return pobj;
}
console.log(hhtprequest(9));
Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
Atik Hashmee
  • 393
  • 3
  • 12
  • `it does not return the object` So what does it return? You have to explain everything if you want help, this tells us nothing – StudioTime Feb 13 '18 at 19:09
  • Okey, first of all thanks for your response . it does return nothing . except the function phototype itself . whatever the fucntion signature and value in it , it does return all . i want it to be returned like javascript object . for example '{"image":image,"name":name,"price":price} ' i know i am making some silly mistake but unable to catch that . – Atik Hashmee Feb 13 '18 at 19:16
  • Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Patrick Roberts Feb 13 '18 at 19:43

3 Answers3

2

Try this..

var pobj = {};
function handleData(obj){
   console.log(JSON.stringify(obj));
}
function hhtprequest(id)
{
    var xhr =  new XMLHttpRequest();
    xhr.open("GET","ajax/productinfforsell.php?pid="+id,true);
    xhr.onload = function(){
        if (this.readyState === 4 && this.status === 200) {
            var data = JSON.parse(this.response);
            pobj.image = data.imagefile;
            pobj.name = data.name;
            pobj.price = data.price;     
            handleData(pobj);         
        }
    }
    xhr.send();
}
kheya
  • 7,546
  • 20
  • 77
  • 109
0
function hhtprequest(id)
{
    var pobj = function(image,name,price){
        this.image = image;
        this.name = name;
        this.price = price;
    }

    var xhr =  new XMLHttpRequest();
    xhr.open("GET","ajax/productinfforsell.php?pid="+id,true);
    xhr.onload = function(){
        if (this.readyState === 4 && this.status === 200) {
            var data = JSON.parse(this.response);
        var obj =new pobj(data.imagefile,data.name,data.price);

        }

    }

    xhr.send();
    return obj ;
}
console.log(hhtprequest(9));
surjeet
  • 1
  • 1
-1
new pobj(data.imagefile,data.name,data.price);

You need to assign this object in a var and then return that var.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
surjeet
  • 1
  • 1
  • Was this intended to be a comment to your answer? FYI, dumping code without explanation doesn't make for quality answers. You need to explain why it solves the problem. – jmoerdyk Feb 13 '18 at 19:53
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/18813067) – Ahmad F Feb 13 '18 at 20:21
  • @AhmadF are you sure it doesnot provide the answer – Muhammad Omer Aslam Feb 13 '18 at 21:48
  • As per given problem user want to return an object but he was returning a function named as "pobj ". He has created an object but not assigned to any variable.So in my solution, I have created a variable and object assigned to this variable. var obj =new pobj(data.imagefile,data.name,data.price); – surjeet Feb 14 '18 at 08:49