Restful web service not responding to ajax - ajax

I have following code:
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://www.example.com/rest/api.php?request=users",
dataType: "json",
}).then(function(data) {
$('.greeting-id').append(data);
//$('.greeting-content').append(data.content);
});
});
But cannot see any data.
When I copy-paste url of web-service in browser, it works fine. any idea?

Related

403 error on Ajax Post Laravel shared hosting

Site works completely OK on other hosting. That is also shared. But doesn't work on current hosting when an Ajax post request is made. The server(not app) responds with 403.
What should I do now? I used postman and it works okay. No problem in url also.
Update:
the code for ajax request:
$.ajax({
type: "POST",
url: window.location.href.split('?')[0],
data: data,
success: function(data){
window.location = data.redirect_to;
},
error: function(data){
},
dataType: 'json'
});
The problem was "not setting" content-type in headers.
I changed the code into:
$.ajax({
type: "POST",
url: window.location.href.split('?')[0],
data: JSON.stringify(data),
success: function(data){
window.location = data.redirect_to;
},
error: function(data){
},
dataType: 'json',
headers: {
'Content-Type':'application/json'
}
});
And it worked.

JsonNetValueProviderFactory does not deserialize JSON from AngularJS post

I have an asp.net MVC application.
In my global.asax:
ValueProviderFactories.Factories.Add(new JsonNetValueProviderFactory());
if I use jquery ajax
$.ajax({
url: urlPostInvitation,
type: 'POST',
dataType: 'json',
data: $.toJSON($scope.invitation),
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert('Yes');
}
});
Everything works fine, but if I use AngularJS ajax
$http({
url: urlPostInvitation,
method: "POST",
data: $scope.invitation,
headers: { 'Content-Type': 'application/json; charset=utf-8' }
}).success(function (data, status, headers, config) {
alert("Yes");
});
I get an empty object in the server.
With JsonValueProviderFactory everything works fine, but not with JsonNetValueProviderFactory
If you output $scope.invitation and $.toJSON($scope.invitation) to the console do they look the same?
I answer myself, the jQuery ajax post adds the header: 'X-Requested-With': 'XMLHttpRequest'
If I add this in the AngularJs call it works

How to consume a rest service hosted on remote server in asp.net mvc3 using jquery ajax ?

I have a rest service. I hosted this service to IIS in my local system and consumed in an mvc3 application. It run without any error. But in the same application, I am not able to consume the rest service hosted on remote server. I have written following jquery to access it :
<script type="text/javascript">
$(document).ready(function () {
$("#btn").click(function () {
$.ajax({
url: "http://portno/Student.svc/AutoComplete?Branch=Civil&RecordLimit=4&s_name=Linda",
type: "GET",
contentType: "application/json; charset=utf-8",
data: {},
dataType: "xml",
success: function (data) {
alert(data.documentElement.childNodes[3].textContent);
},
error: function (error) {
alert("Error: " + error.text);
}
});
});
});
is there any mistake here? Please help me. Thank you.

In MVC calling webservice with ajax call not working give error code 404?

In my .net framework with MVC calling webmethod like. webservice1.asmx/helloWorld
with Ajax give error 404 not found..In my another server same code working. Is there
anything missing to call ?? and physical path give me same webserive and webmthod in my .net project.. please help me ..
EDIT
code to call the web service
$.ajax({
type: "POST",
url: "/WebServices/WebService1.asmx/HelloWorld",
data:"{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(msg) {
var data = msg.d;
},
error: function(msg) {
alert(msg);
}
});
I suspect that you have hardcoded the url to the web service in your javascript call instead of using an url helper to generate it. So try like this:
<script type="text/javascript">
$.ajax({
type: "POST",
url: "#Url.Content("~/WebServices/WebService1.asmx/HelloWorld")",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(msg) {
var data = msg.d;
},
error: function(msg) {
alert(msg);
}
});
</script>
Notice how the url to the web service is no longer /WebServices/... but it is generated with an url helper. So if for example you deploy your application in a virtual directory in IIS the helper will take into account this virtual directory.

Basecamp API Request results in Access-Control-Allow-Origin Error

I am trying to perform an AJAX request from a Chrome Extension to Basecamp to authenticate in so I can pull tasks. I have added https://site.basecamphq.com to the permissions in manifest.json. However, when this function is executed, I get this in my console:
XMLHttpRequest cannot load https://site.basecamphq.com. Origin chrome-extension://0123456789 is not allowed by Access-Control-Allow-Origin
$("#login").click(function()
{
$.ajax({
type: "GET",
dataType: 'html',
url: "https://site.basecamphq.com",
username: "username",
password: "X",
success: function(data){
$("#example").append(data);
}
});
});
I have added https://*/ to my manifest.json permissions as well, but no luck.
You need to use background page for doing AJAX requests from a content script.
Background page code:
chrome.extension.onRequest.addListener(function(request, sender, callback) {
$.ajax({
type: "GET",
dataType: 'html',
url: request.url,
username: "username",
password: "X",
success: callback
});
});
Content script code:
chrome.extension.sendRequest({'url': 'https://site.basecamphq.com'}, function(data) {
$("#example").append(data);
});

Resources