From my work gave me a project to finish with which I am having the following problem:
In solution there is web API and RESTful API in MVC
if I call directly Restful service it's responding
if i make a test and put in home controller in web API HttpWebResponse it is responding
but the request is put in .js file and the code is
return $.ajax({
type: 'GET',
url: serviceURL,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: urlParams,
crossDomain: true,
beforeSend: lazy(),
complete: complete()
});
and this is failing with the following message e requested resource does not support http method 'OPTIONS
in webconfig everything is settled correctly
It is not working if I call REST API locally, if I call those which is on server it is working.
I read almost everything connected to that problem, but I am still stuck.
Related
I have problem with AJAX call. error with parseerror. my code:
$.ajax({
type: "GET",
url: "http://localhost:8089/SpringNew/tesget",
dataType: "jsonp",
contentType: "application/json; charset=utf-8",
success: function(resp){
alert("Server said123:\n '" + resp );
},
error: function(request, errorText, errorCode){
alert('Error121212: ' + errorText);
}
});
When I use this code, error with alert "Error121212: parseerror", and when I use dataType: "json", I have error: "XMLHttpRequest cannot load".
I created web services with Java Spring, in webservices, I run in port 8089, and in frontend (I use SAPUI5) in port 8080.
my web services:
How to fix this problem?
Thanks.
Bobby
The question seems to have missed some details. I believe this is because of the CORS headers from the server side. You should allow the origin that you are requesting with.
For allowing all, you can set the header 'Access-Control-Allow-Origin' to be '*'
This should solve your issue.
Your datatype contains an typo imho. It should be "json" instead of "jsonp". You may also try ...[url to service]/[entityset]?$format=json
My Rails application is accessing MongoLab DB that has data loaded to it.
I am making a AJAX request to my DB. Here is the code:
$.ajax({
type: "GET",
url: "mongodb://heroku_rgkq33fl:kbi2jctr39ts8gicrrchjuso5s#ds061954.mongolab.com:61954/heroku_rgkq33fl",
success: function(data){
console.log("Success 1234");
console.log(data);
},
error: function(data)
{
console.log("Error");
}
})
}
I am not able to make a AJAX request because of the following error:
XMLHttpRequest cannot load mongodb://heroku_rgkq33fl:kbi2jctr39ts8gicrrchjuso5s#ds061954.mongolab.com:61954/heroku_rgkq33fl. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
Can someone please help me on how to get the data from my DataBase to my html page.
I am trying to call an asmx web service in plain html page. But not getting output. I'm not getting the exact problem. Here is my ajax code that I wrote to call that asmx web service.
function Getdet(Name)
{
alert('hellotest');
$.ajax({
type: "POST",
url: "http://192.168.1.20/myservice/service.asmx?HelloWorld", // add web service Name and web service Method Name
data: "{''}", //web Service method Parameter Name and ,user Input value which in Name Variable.
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response)
{
alert('abcd1');
$("#spnGetdet").html(response.d); //getting the Response from JSON
},
failure: function (msg)
{
alert(msg);
}
});
}
Should I have to add anything in the code..?? Please let me know where I am going wrong in this above code.
ASMX pages are plain old Web Services and the output is (almost) always in XML format, so don't except JSON out of the box.
Plus, they don't work well on different domain call by default, you will need to ensure that the cross domain is correctly handled.
After all this is done, you will then use the call as:
success: function(response) {
// http://stackoverflow.com/a/1773571/28004
var xml = parseXml(response),
json = xml2json(xml);
$("#spnGetdet").html(json.d);
}
Here is my little first project as practice, having real trouble figuring out how to use JSON, took me a while to get it work locally but still no luck with servers, and tried few a including one i hosted, and even tried it with other hosted json files.
http://jsfiddle.net/Atlas_/Mgyc5/1/
$.ajax({
dataType: "json",
async: false,
url: "package.json", //https://www.dropbox.com/s/fmw63i4v7dtnx6t/package.json
'success': function (json) {
theQuiz = json.quiz;
console.log(json);
console.log(theQuiz);
}
});
When you access another domain be carefully with "Crossdomain".
To use with dropbox, try changing the URL to:
https://dl.dropboxusercontent.com/s/fmw63i4v7dtnx6t/package.json
Your code will be like this:
$.ajax({
dataType: "json",
async: false,
url: "https://dl.dropboxusercontent.com/s/fmw63i4v7dtnx6t/package.json",
success: function (json) {
theQuiz = json.quiz;
console.log(json);
console.log(theQuiz);
}
});
When you request 'dl.dropboxusercontent.com', you have this:
Access-Control-Allow-Origin: *
https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control
"In this case, the server responds with a Access-Control-Allow-Origin:
* which means that the resource can be accessed by any domain in a cross-site manner."
Another options: Some websites (twitter, for example) work with "jsonp". http://en.wikipedia.org/wiki/JSONP or ..you can create your own proxy.
solved - it was Adblock Plus
I have a cross-domain OAuth scenario where I'm calling OAuth protected jsonp resources on a 3rd party api directly from JavaScript.
The client relies on my server (the site of origin) to generate signed request urls for OAuth resources on the provider. The client GETs these OAuth resources directly in an attempt to limit OAuth proxy duties of my server to simply generating signed resource urls and letting JS do the rest. Here's the JS:
myNamespace = {
callApi: function(signedRequest, callback) {
$.ajax({
url: signedRequest,
dataType: "jsonp",
type: "GET",
crossDomain: true,
jsonp: false,
jsonpCallback: callback,
cache: true,
async: true
});
},
usersCallback: function(jsonp) {
alert(jsonp);
},
getOAuthResource: function(resource, callback) {
$.ajax({
url: "/GetSignedRequest?resource=" + encodeURIComponent(resource + "&callback=" + callback),
success: function (signedRequest) {
myNamespace.callApi(signedRequest, callback);
}
});
}
}
Because the parameter order matters when signing the request, I include the callback manually so that it gets ordered & included in the signed request the server generates.
So an example usage would be simply,
myNamespace.getOAuthResource("/users?id=1", "myNamespace.usersCallback");
This is working in FireFox 20 and IE 10 - I get the alert msg & the expected json from the provider.
Chrome 26 refuses to perform the cross-domain call to the provider. In dev tools, the GET for the cross-domain resource immediately shows a Status of "(failed)" and the GET shows up in the console as an error.
If I subsequently ask Chrome to open this supposedly "failed" URL in a new window, it re-requests the URL directly on the provider and I get the expected jsonp response:
myNamespace.usersCallback({...})
Why won't Chrome perform the cross-domain call?