0

I have created a MVC 4 Web API, which expect a parameter. This works fine when I directly call it by putting url in browser. Now I am trying to call this API with data and with complete url(with parameter) both , through jquery on an aspx page . My code looks like

$.ajax
            ({
                type: "GET",
                url: 'http://MySite/Test/api/product/?context=001',
                dataType: "jsonp",
                //data:{"context": "001")},
                crossDomain : true,
                async:false,
                success: function (result) {
                    alert(result);
                },
                error: function (data) {
                    alert(data);
            }
            });

But always I get response in 'error' function as [object Object]. When I expand the returned object I found following

Object
abort: function (a){a=a||"abort",p&&p.abort(a),w(0,a);return this}
always: function (){i.done.apply(i,arguments).fail.apply(i,arguments);return this}
complete: function (){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this}
done: function (){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this}
error: function (){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this}
fail: function (){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this}
getAllResponseHeaders: function (){return s===2?n:null}
getResponseHeader: function (a){var c;if(s===2){if(!o){o={};while(c=bH.exec(n))o[c[1].toLowerCase()]=c[2]}c=o[a.toLowerCase()]}return c===b?null:c}
isRejected: function (){return!!e}
isResolved: function (){return!!e}
overrideMimeType: function (a){s||(d.mimeType=a);return this}
pipe: function (a,b,c){return f.Deferred(function(d){f.each({done:[a,"resolve"],fail:[b,"reject"],progress:[c,"notify"]},function(a,b){var c=b[0],e=b[1],g;f.isFunction(c)?i[a](function(){g=c.apply(this,arguments),g&&f.isFunction(g.promise)?g.promise().then(d.resolve,d.reject,d.notify):d[e+"With"](this===i?d:this,[g])}):i[a](d[e])})}).promise()}
progress: function (){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this}
promise: function (a){if(a==null)a=h;else for(var b in h)a[b]=h[b];return a}
readyState: 4
setRequestHeader: function (a,b){if(!s){var c=a.toLowerCase();a=m[c]=m[c]||a,l[a]=b}return this}
state: function (){return e}
status: 200
statusCode: function (a){if(a){var b;if(s<2)for(b in a)j[b]=[j[b],a[b]];else b=a[v.status],v.then(b,b)}return this}
statusText: "success"
success: function (){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this}
then: function (a,b,c){i.done(a).fail(b).progress(c);return this}
tereško
  • 58,060
  • 25
  • 98
  • 150
Haseeb Akhtar
  • 1,233
  • 3
  • 16
  • 32
  • What is actual output when you call through browser? and does it require any credentials to call the API? – Furqan Hameedi Jun 26 '12 at 06:59
  • In ie I get json object as well as in Chrome xml and it is as expected. In url ...?context=001 is my parameter. – Haseeb Akhtar Jun 26 '12 at 07:07
  • try with changing the `dataType: "json xml"` . – Furqan Hameedi Jun 26 '12 at 07:18
  • as per `$.ajax` documentation `Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation`, so u can remove it from your request. Additionally please also check `JSONP` and `jsonpCallback` attributes on this `http://api.jquery.com/jQuery.ajax/` – Furqan Hameedi Jun 26 '12 at 07:29

1 Answers1

0

I came across a similar article on Stackoverflow itself which talks about calling a MVC 4 Web API from jQuery. Below is the link for the same,

JSONP with ASP.NET Web API

Community
  • 1
  • 1
Amit Tyagi
  • 101
  • 3