【发布时间】:2012-06-26 06:57:15
【问题描述】:
我创建了一个 MVC 4 Web API,它需要一个参数。当我通过将 url 放入浏览器中直接调用它时,这很好用。现在我试图通过aspx页面上的jquery用数据和完整的url(带参数)调用这个API。我的代码看起来像
$.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);
}
});
但我总是在“错误”函数中作为 [object Object] 得到响应。 当我展开返回的对象时,我发现如下
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}
【问题讨论】:
-
浏览器调用的实际输出是什么?调用 API 是否需要任何凭据?
-
在即我得到 json 对象以及在 Chrome xml 中,它符合预期。在 url ...?context=001 是我的参数。
-
尝试更改
dataType: "json xml"。 -
根据
$.ajax文档Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation,因此您可以将其从您的请求中删除。另外,请检查http://api.jquery.com/jQuery.ajax/上的JSONP和jsonpCallback属性
标签: asp.net-mvc c#-4.0 jquery asp.net-web-api