I am working with ASP.NET 4.0 framework. In one web page, I have to call web service using Jquery as
var serviceurl = 'http://www.websitename.com/webservicename';
$.ajax({
type: "POST",
url: serviceurl + 'WebServiceName',
data: "{'Parameters': '" + parameter+ "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
ShowAfterSuccess(msg);
},
error: AjaxFailed
});
It works fine, if i mention url as "http://www.websiteName.com" but when i put URL as "websitename.com" it doent call webservice.
but it works well only in Google Chrome with "websiteName.com" I dont knw what is the issue with that....whether there is problem in my webservice calling or in URL..
You must ensure that you are not violating the same origin policy restriction. The best way to ensure this is to use relative urls:
var serviceurl = '/webservicename';
You must ensure that the domain hosting this javascript matches exactly the domain that you are sending your AJAX call to (including the protocol).
Related
I'm having a problem to make a call to a rest API.
In the document (FAQ) of the web application there is an example that use AJAX request to make the call. Here an example:
var url = 'https://example.com/yyy';
$.ajax({
type: 'POST',
url: url,
cache: false,
data: {
opt: JSON.stringify(example)
},
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (result)
{
console.log(result);
} });
I created a local page with this code to made the post to the API that is located on a remote server but I receive an error about CORS.
Is there any solution to circumvent this problem? I tried to use firefox plugin to allow CORS but it didn't solve the problem. The session is authenticated via form before use the endpoint.
I see several issues:
Try to run the code from a domain and not from local disk (alternatively you can consider using https://crossorigin.me/ )
How does the authentication work? if with cookies you need to add withCredentials to the ajax request.
Make sure the API returns Access-Control-Allow-Origin: foo header where foo is the domain your code runs in. If you also used withCredentials, you should add Access-Control-Allow-Credentials: true
I'm trying to use the google chart api in an XPages application.
I'm using the code example given by the documentation : https://developers.google.com/chart/interactive/docs/php_example#exampleusingphphtml-file
I have to replace the call to the php page by a call to an LS agent.
var jsonData = $.ajax({
url: "getData.php",
dataType: "json",
async: false
}).responseText;
So my code goes to :
var jsonData = $.ajax({
url: "http://server/database/agent?openagent",
dataType: "json",
async: false
}).responseText;
On my local domino server, it works fine.
On the production domino server, I get nothing. The chart is not drawn. After debugging the js client side, it seems the ajax call is expecting an authentification even if I had to log in before.
The anonymous access is not allowed on both servers.
The security level seems to be same on both environments
Any help will be welcome (or any other way to proceed if I'm wrong).
Thank you
If you are able to draw the google chart in your local server, but not in production server, this means it is your server issue.
You can add authentication header in your jquery ajax call to make authenticated ajax request
$.ajax({
headers: {
"Authorization": "Bearer <TOKEN HERE>"
}
})
You can also send username and password in jquery ajax call, to make authenticated request. Here is the sample code from the link
$.ajax({
type: 'GET',
url: 'url',
dataType: 'json',
//whatever you need
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', make_base_auth(user, password));
},
success: function () {});
});
function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = btoa(tok);
return 'Basic ' + hash;
}
at the end, I tried to run the ajax request through dojo instead of Jquery.
My codes became this one :
var jsonData = dojo.xhrGet({
url: "http://server/database/agent?openagent",
handleAs:"json",
...
})
I did no changes at the security level or anything else.
I do not understand why the jquery syntax is not working as well the dojo syntax.
anyway, it is working now.
Many thanks to all for your suggestions
I have created a WebService directory in my MVC web application's root folder and created a webservice named MyService.asmx. In this file, I created an method UpdateReadCount, which I call from javascript with below code.
function updateReadCount(contentID) {
$.ajax({
type: "POST",
url: "/WebServices/MyService.asmx/UpdateReadCount",
data: "{'contentID': '" + contentID + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (json) {
//$("#" + messageView).html(json.d);
}
});
}
I am getting Page Not Found error. This is happening at hosted page, while it works perfectly on localhost.
Why use a .asmx with an MVC application? You can just do a clean controller action for this.
I am using JQuery Ajax method and Ajax to talk to a ASP.NET web service.
I have such a setup in my javascript app.
var parameters='Token="'+psToken+'"&ID="'+psDID+'";
$.ajax({
type: "GET",
url: webMethod,
data: parameters,
dataType: "jsonp",
success: function(msg) {
XXXXX.XX(msg.X);
},
As per the IIS log, I can see that all Encoding is happening right and all instances of double quotes get a %22.
The problem is that when for example psToken contains a double quote i.e " , then the web service crashes and I get a 500 error. It doesnt even get to the point of hitting the web methods, hence I cant debug and figure out the error.
What could I be doing wrong.
If the using of jsonp data type is not critical for you, use json dataType with following code:
var parameters = {
'Token': psToken,
'ID': psDID
};
$.ajax({
type: "GET",
url: webMethod,
data: JSON.stringify(parameters),
dataType: "json",
success: function(msg) {
XXXXX.XX(msg.X);
},
I would like to call OData .NET web service that authenticates users via basic authentication.
I use following ajax call:
var fullUri = APIUri + "?$format=json";
$.ajax({
url: fullUri,
contentType: "application/json",
dataType: "jsonp",
type: 'GET',
jsonp: '$callback',
beforeSend: function setHeader(xhr) {
xhr.setRequestHeader('Authorization', token);
},
success: callback,
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError);
},
});
The results are unusable for me:
Calls are blocked because of CORS (until I will paste API url and try to load it in chrome). I tried local html file and html file uploaded to the same domain/port, but authentication fails (according to Chrome console).
Once I enter service URL into chrome address bar, I am asked to provide login name and password by Chrome. If I enter them, they are cached and used even I assign them in beforeSend. How to blocks this behavior?
I've tried a lot of examples how to configure jsonp, headers etc, but did not find working solution yet.
IIS server response header is also configured using "Access-Control-Allow-Origin" value="*".
You can set the HTTP Password and Username in the AJAX Call directly:
$.ajax({
url: fullUri,
contentType: "application/json",
username: <login>,
password: <password>,
...
Use the following to support CORS:
jQuery.support.cors = true;
Regarding the call, are you using HTTPS? Is the certificate valid?