-2

On posting to server: i got jsonp({"code":"680","country":"UK","status":"Citizen"}); i.e the content of result and am trying to access the value of country.

My attempt was creating my own jsonp function to access it.

done(function (result, status, xhr){

   console.log(result); 

    //log result is: 

    //jsonp({"code":"680","country":"UK","status":"Citizen"});
     transfer(result);
})

//outside of jquery call; i have this outside the click function.
function transfer(result){
   var xyz = result;
   function jsonp(xyz){
     alert(xyz.country);
   } 
 }

I don`t get to access the county in jquery done function.; In a nuetral script this works i.e creating and html file for testing.

<script>
 var result = jsonp({"code":"680","country":"UK","status":"Citizen"});

  jsonp(result){
      alert(result.country);          // alert is UK
      console.log(result.country);   //result is UK.
  }
  </script>

Thank you.

Haji
  • 17
  • 3
  • I do not clearly understand your goal, where is called function jsonp? – Plastic Dec 12 '19 at 17:19
  • I think the result is a string that comes from the response of a server. So, you need to use eval or exec to call the function. – mahadev kalyan srikanth Dec 12 '19 at 17:20
  • If you're receiving a JSONP formatted response you need to set the `dataType` in the `$.ajax()` call to `'jsonp'`. jQuery will then deserialise the object for you, completely avoiding the problem you have right now – Rory McCrossan Dec 12 '19 at 17:21
  • Does this answer your question? [Basic example of using .ajax() with JSONP?](https://stackoverflow.com/questions/5943630/basic-example-of-using-ajax-with-jsonp) – Heretic Monkey Dec 12 '19 at 17:46
  • @Plastic, what i meant was after the return from the server: jsonp({"code":"680","country":"UK","status":"Citizen"}); i want to have the access to the country. i.e UK. inside of done success. Looking at the response returned, i created a function jsonp(obj){ console.log(obj.country); }. In all nothing happened, no console, no warning. Thank you. – Haji Dec 13 '19 at 13:01
  • @Rory McCrossan, i did set the dataType to jsonp. – Haji Dec 13 '19 at 13:03
  • @Haji then please show all of your relevant code so we can fully understand what you're going – Rory McCrossan Dec 13 '19 at 13:07
  • }).done(function (result, status, xhr) { var tya = result; console.log("Doing"); function jsonp(xyz){ alert(xyz.country); } console.log("Done"); The alert function returns nothing, but i got console for Doing and Done. – Haji Dec 13 '19 at 13:09

1 Answers1

-1

You can do this.

    done(function (result, status, xhr){

       console.log(result); 

       //log result is: 

       //jsonp({"code":"680","country":"UK","status":"Citizen"});
       eval(result);
    })



    function jsonp(xyz){
      alert(xyz.country);
    } 

Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53