Asp.net WebAPI - JSONP parser error

41 views
Skip to first unread message

Google User

unread,
Aug 16, 2015, 11:57:08 AM8/16/15
to phonegap
Hi,

I am using default WebAPI asp.net project which returns string value when call GET method. When I invoke this method from Phone Gap (Visual Studio Windows Phone Emulator), it always returns method not found error for jsonp. Here is my code

$(document).on('pageinit', '#home', function(){
      $.support.cors = true;
      $.mobile.allowCrossDomainPages = true;

$(document).on('click', '#search', function(){
     var url = 'http://localhost/api/values/5';
      $.ajax({
      method: "get",
      url: url,
      contentType: "application/json; charset=utf-8",
      dataType: "jsonp",
      async:false,
      crossDomain: true,
      cache: false,
      jsonp:'jsonpCallback',
      jsonpCallback:'infoData',
      success: function (result) {
      alert('success');
      },
      error: function(xhr,textStatus,errorThrown) {
      alert(xhr.responseText);
      alert(textStatus);
      alert(errorThrown);
      }
      });
      });
      });

function infoData(jsonObj) {
      alert(JSON.stringify(jsonObj));  // print the raw JSON object
      alert(jsonObj.validJsonDocument); // access the property in your JSON object.
      }

Kerri Shotts

unread,
Aug 16, 2015, 12:17:49 PM8/16/15
to phonegap
1) Avoid JSONP if at all possible -- it's incredibly insecure. 
2) Make sure `jsonp:'...'` is using the right JSONP callback parameter name and is something your server is going to understand
3) Why not handle the response in `success`?


Google User

unread,
Aug 16, 2015, 12:36:09 PM8/16/15
to phonegap
Hello Kerri,

Thanks for your reply. Here is my comments:

(1) What is the alternate approach to call Web API to replace JSONP call?
(2) I have already attached call, can you please guide me what is wrong in that code, my WEB API simply return string data type.


public class ValuesController : ApiController
    {
        // GET api/values/5
        public string Get(int id)
        {
            return "value";
        }


(3) There is already success and error methods but they are not being invoked by WEB API when I try to invoke from phone emulator. It trigger when I call from web page.

Thanks,
Kumar
Reply all
Reply to author
Forward
0 new messages