can u please make sure jQuery .js is attached and you create an instance of jQuery on document complete event of page and then you are calling these lines are code e.g
$(document).ready(function() {
// bind your event here or call it here if you are calling it directly after load
});
JSONP only does GET requests. You need to look into CORS if you want to support POST, PUR or DELETE across origins. CORS is not built into WebAPI, so you'll have to do it yourself or find a library that can help you, such as this one:
PMA
0 Points
6 Posts
How to make an ajax call to a cross-domain Web API with a complex parameter(object)
Dec 07, 2012 09:29 PM|LINK
I am struggling to make an ajax call to a cross-domain web API which requires a complex parameter(object). Here is the script:
var o = {
x: 1,
y: 2
};
$.ajax({
url: ("http://localhost:45327/api/test"),
contentType: "application/json; charset=utf-8",
data: JSON.stringify(o),
dataType:"jsonp",
type: "post",
success: function (result) {
alert(result);
},
error: function (xhr, txt, err) {
alert("error connecting to data: " + txt);
}
});
But the above ajax call fails and error is "jquery........ is not called". Can we pass a complex parameter to a cross-domain Web API. If yes, how?
I could make it work if the web page comes from the same domain as the web API though.
JangZ
Member
207 Points
74 Posts
Re: How to make an ajax call to a cross-domain Web API with a complex parameter(object)
Dec 07, 2012 10:12 PM|LINK
can u please make sure jQuery .js is attached and you create an instance of jQuery on document complete event of page and then you are calling these lines are code e.g
$(document).ready(function() { // bind your event here or call it here if you are calling it directly after load });PMA
0 Points
6 Posts
Re: How to make an ajax call to a cross-domain Web API with a complex parameter(object)
Dec 07, 2012 10:47 PM|LINK
BrockAllen
All-Star
28084 Points
4997 Posts
MVP
Re: How to make an ajax call to a cross-domain Web API with a complex parameter(object)
Dec 07, 2012 11:36 PM|LINK
JSONP only does GET requests. You need to look into CORS if you want to support POST, PUR or DELETE across origins. CORS is not built into WebAPI, so you'll have to do it yourself or find a library that can help you, such as this one:
http://brockallen.com/2012/06/28/cors-support-in-webapi-mvc-and-iis-with-thinktecture-identitymodel/
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
Ankit Purani...
Member
334 Points
65 Posts
Re: How to make an ajax call to a cross-domain Web API with a complex parameter(object)
Dec 13, 2012 08:05 AM|LINK
Try putting this at the start of your jquery function:
$.support.cors = true;
Hope this helps.
Ankit
My .NET Blog
(Please Mark As Answer, if this helps you)
PMA
0 Points
6 Posts
Re: How to make an ajax call to a cross-domain Web API with a complex parameter(object)
Dec 18, 2012 07:48 PM|LINK
It works. Thanks!
Ankit Purani...
Member
334 Points
65 Posts
Re: How to make an ajax call to a cross-domain Web API with a complex parameter(object)
Dec 19, 2012 01:18 PM|LINK
Kindly mark the post as answer which ever worked for you :)
Ankit
My .NET Blog
(Please Mark As Answer, if this helps you)