I am trying to access a WebAPI (in the same server but different IP). It works like a charm in IE 10 . But in IE 8 it goes worse. I have included $.support.cors=true and also included the jQuery.XDomainRequest.js which I got from
https://github.com/MoonScript/jQuery-ajaxTransport-DomainRequest/blob/master/jQuery.XDomainRequest.js
For the GET request it is working, but for the post its throwing out error. I learned that for POSt content type should be text/plain.
I tried to send my data as a plain text, probably the server is not parsing it properly.
I also tried jsonp as well, but didn't work
I am putting down my webapi call . Please suggest on how can I get this working. Thanks a lot .
function Authenticate() {
var UserInfoRequest = {
Email: $("#txtEmail").val(), SubDomain: subDomainName
};
//UserInfoRequest ="Email="+$("#txtEmail").val()+"&SubDomain="+subDomainName;
$.ajax({
url: defaultAPIurl + "Login/UserExistOrDualRole" ,
type: "POST",
dataType: 'json',
data: UserInfoRequest ,
success: function (data) {
//do something
},
error: function (data) {
Showerror();
return;
}
});
return false;
}
ASP.NET WEB API support CROS need extra library. Check this post:CORS support for ASP.NET Web API
Modified your project as this post written,it's will be working.
tips:The two library at their NightBuild Nuget server when I used it.
Related
this is the below code which i am using for delete functionality.At Server side there is RestAPICall. But i can see the request is not at all going to back-end and neither i am getting any error in IE.It's working in FF,Chrome but not in IE9.
var delURL = "url call to restful webservices";
$.ajax({
type: "DELETE",
url:delURL,
contentType: "application/json",
data: sendUserData(),
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (data) {
console.log("inside error method->" + data);
}
});
var sendUserData = function(){
return JSON.stringify(userData);
};
Everytime when i am trying to delete some records there is no request is going to back-end, instead immediately the alert statement in error method will get executed.
I had tried and googled some blogs. But not able to get the solution.
Before marking duplicate Let me list down what all i tried:-
1.IE does not support JSON object (e.g.: JSON.stringify()) but the json2.js plugin should compensate for that, and I see that is included in my code. So I have not found the answer yet why still not working.
JSON.stringify and JSON.parse not working in IE9?
JSON.stringify does not work in IE
2.I also read that Jquery.Ajax with delete method not working in IE.
Problem with jQuery.ajax with 'delete' method in ie
I create an account with yaler, to comunicate with my arduino yun. It works fine, and i'm able to switch on and off my leds.
Then i created a web page, with a button that calls an ajax function with GET method to yaler (yaler web server accept REST style on the URL)
$.ajax({
url: "http://RELAY_DOMAIN.try.yaler.net/arduino/digital/13/1",
dataType: "json",
success: function(msg){
var jsonStr = msg;
},
error: function(err){
alert(err.responseText);
}
});
This code seem to work fine, infact the led switches off and on, but i expect a json response in success function (msg) like this:
{
"command":"digital",
"pin":13,
"value":1,
"action":"write"
}
But i get an error (error function). I also tried to alert the err.responseText, but it is undefined....
How could i solve the issue? Any suggestions???
Thanks in advance....
If the Web page containing the above Ajax request is served from a different origin, you'll have to work around the same origin policy of your Web browser.
There are two ways to do this (based on http://forum.arduino.cc/index.php?topic=304804):
CORS, i.e. adding the header Access-Control-Allow-Origin: * to the Yun Web service
JSONP, i.e. getting the Yun to serve an additional JS function if requested by the Ajax call with a query parameter ?callback=?
CORS can probably be configured in the OpenWRT part of the Yun, while JSONP could be added to the Brige.ino code (which you seem to be using).
I had the same problem. I used JSONP to solve it. JSONP is JSON with padding. Basically means you send the JSON data with a sort of wrapper.
Instead of just the data you have to send a Java Script function and this is allowed by the internet.
So instead of your response being :
{"command":"digital","pin":13,"value":0,"action":"write"}
It should be:
showResult({command:"analog",pin:13,value:0,action:"write"});
I changed the yunYaler.ino to do this.
So for the html :
var url = 'http://try.yaler.net/realy-domain/analog/13/210';
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'showResult',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.dir(json.action);
},
error: function(e) {
console.log(e.message);
}
});
};
function showResult(show)
{
var str = "command = "+show.command;// you can do the others the same way.
alert (str);
}
My JSON is wrapped with a showResult() so its made JSONP and its the function I called in the callback.
Hope this helps. If CORS worked for you. Could you please put up how it worked here.
I am trying to get CasperJS to post a form using AJAX - it doesn't seem to work for me, the code is below:
this.then(function() {
response = this.evaluate(function() {
params = $("#offer").serialize();
//require('utils').dump(params);
$.ajax({
type: "POST",
url: 'http://www.example.com/getoffer.php',
data: params,
success: function (data) {
//return data.responseText;
return __utils__.sendAJAX(url, 'POST', params);
},
error: function (xhr,status,error){
return error;
}
});
});
this.echo(response);
});
CORS?
(I'm so tempted to just leave that as my shortest ever StackOverflow answer :-)
Your JavaScript is being executed from inside the browser, and the security model will apply. Your "origin" will be the page that CasperJS is requesting; if that is not "www.mysite.com" (or if it is but is HTTPS), then the browser will refuse to send it.
This answer https://stackoverflow.com/a/16221536/841830 says --web-security=false (give that as a casperjs commandline option) will get around CORS restrictions.
This issue seems to be doing the same as you, so if it is not a CORS problem, it might give you some other ideas: http://code.google.com/p/phantomjs/issues/detail?id=28
I am calling the web service from other domain using Ajax call and I want to get returned response from server in my application by using following code I get response text in firebug but not in my JavaScript code. Control are not showing success and error response it goes out directly.
I want response in my success or error section but both not handling in this.
I am trying lot but not finding any solution please any one help me.
I am in a trouble. I hope somebody can help me for calling cross domain web service by using Ajax call. I am trying from 1 week but didn't find any solution till. I am getting response on browser but not getting it on my actual code.
My JavaScript code.
crossdomain.async_load_javascript(jquery_path, function () {
$(function () {
crossdomain.ajax({
type: "GET",
url: "http://192.168.15.188/Service/Service.svc/GetMachineInfo?serialNumber="+123,
success: function (txt) {
$('#responseget').html(txt);
alert("hii get");
}
});
crossdomain.ajax({
type: "POST",
url: "http://192.168.15.188/Server/Service.svc/GetEvents/",
// data: "origin=" + escape(origin),
success: function (txt) {
$('#responsepost').html(txt);
alert("hii post");
}
});
});
});
</script>
You can't simply ignore the Same Origin Policy.
There are only three solutions to fetch an answer from a web-service coming from another domain :
do it server-side (on your server)
let the browser think it comes from the same domain by using a proxy on your server
change the web service server, by making it JSONP or (much cleaner today) by adding CORS headers
i have wsdl file and i need get data from. How i can do this ? i'm trying do this with ajax
like this:
jq.ajax({
url: 'http://url.wsdl',
type: 'get',
success: function(data){
alert("OK " + data);
},
error: function (x, y, z) {
alert("ERROR");
}
});
what i do wrong ?
Any other way to get data from wsdl web service using javascript, jquery and etc. is?
I think what you are missing is a data: {}
I read that there was some sort of bug if you did not include that when using $.ajax
Oh, and most likely you are going to need dataType: "json" or whatever datatype the service is using.
Here is an example i am using against an online webservice:
jQuery.support.cors = true; //enables cross domain queries for Ajax
$('#jqueryBtn').click
(function ()
{
$.ajax
(
{
type: "GET",
url: "http://www.webservicemart.com//uszip.asmx/ValidateZip",
data: { 'ZipCode': '22553' },
dataType: 'html',
success: jqSuccess,
error: jqError
}
);
}
Hopefully you can use this example to fix your own code
http://forum.jquery.com/topic/jquery-ajax-to-call-a-wsdl-web-service
The following link should explain why you cannot use AJAX for cross domain queries:
http://www.w3schools.com/xmL/xml_parser.asp:
Access Across Domains
For security reasons, modern browsers do not allow access across
domains.
This means, that both the web page and the file it tries to load,
must be located on the same server.
The examples on W3Schools all open XML files located on the W3Schools
domain.
If you want to use the example above on one of your web pages, the file you load must be located on your own server.
You can create a proxy web page in your web server to access to WSDL web service and return result to the AJAX request