Calling WCFservice from ajax - NS_Error_failure - ajax

I have a problem with my WCF service. I want to call it after some action. Here is ajax call:
$.ajax({
type: "Get",
async: false,
url: 'service_adress/DoWork',
success: function () {
alert("Success");
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
alert(errorThrown);
}
});
Method do work should return string . When im putting service_adress/DoWork into adressbar in browser im getting correct response(< string>Something< /string>), but my ajax is throwing error NS_Error_failure. What is wrong here?

Related

getting 403 forbidden error when using ajax with csrf enabled in codeigniter

I am using codeigniter with csrf enabled. i am making some ajax post requests but somehow i am getting 403 post forbidden error. my folder structure is like this i have included this js in which ajax code is written outside of application folder. the code i am using for ajax request is
var data = {
name: $('.name').val(),
crm_csrf_token: $('input[name="crm_csrf_token"]').val()
}
var url = 'http://demo/signup/signup';
$.ajax({
url: url,
dataType: 'json',
type: 'post',
contentType: 'application/json',
data: data,
success: function( data, textStatus, jQxhr ){
console.log(data);
console.log(textStatus);
console.log(jQxhr);
},
error: function( jqXhr, textStatus, errorThrown ){
console.log(jqXhr);
console.log(textStatus);
console.log(errorThrown);
}
});
so where am i going wrong. before making this ajax call i am validating form using javascript too. site_url() and base_url is not accessible outside application forlder too.
you just need to send csrf token via using jquery cookie you can download it from here
https://github.com/js-cookie/js-cookie
now in your ajax call
$.ajax({
url:url,
data:{
"<?php echo $this->security->get_csrf_token_name(); ?>": Cookies.get('your_csrf_cookie_name_in_config')
},
method :"POST",
success:function(data){
$("#city").html(data);
}
});
Try this working for me, I guess its problem with your url. Here i correct this, might be work for you.
JQuery
var data = {
name: $('.name').val(),
crm_csrf_token: $('input[name="crm_csrf_token"]').val()
}
//Url should be index.php/YourControllerName/YourMethodName
var url = '<?php echo base_url(); ?>index.php/demo/signup';
$.ajax({
url: url,
dataType: 'json',
type: 'post',
contentType: 'application/json',
data: data,
success: function( data, textStatus, jQxhr ){
console.log(data);
console.log(textStatus);
console.log(jQxhr);
},
error: function( jqXhr, textStatus, errorThrown ){
console.log(jqXhr);
console.log(textStatus);
console.log(errorThrown);
}
});
CI Controller :
<?php
class demo extends CI_Controller {
public function signup()
{
echo 'Hello World!';
}
}
Greetings!

Ajax POST request to ODATA service throws Invalid HTTP status code 501

I have the following code which I wish to use for inserting a record into an entity of an ODATA service.
The post request throws a XMLHttpRequest cannot load Invalid HTTP status code 501.
Apparently GET request works fine. Can anyone suggest a way to find out the problem ? Does my WCF Service has a problem ?
var data = { application_id: 777 , user_id: 'test' };
var data = JSON.stringify(data);
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
datatype: 'json',
url: oDataURL + 'application_admins',
data: data,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
account = data.d;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
}
});

jQuery Ajax request error function issue

I'm making an Ajax request that works perfectly fine. The issue I'm having is that I attached the "error" method onto the call and when I modify the URL to try to make it throw an error....I get no error. Here is my code:
function instagramAjax () {
$.ajax ({
url: "url that works",
dataType: "jsonp",
success: function(returnedData) {
//my success code that works
},//end success
error: function(jqXHR, textStatus, errorThrown) {
console.log("Error " + errorThrown);
}
});//end ajax request
}//end instagramAjax function
What is the url that you'd set?
This handler is not called for cross-domain script and cross-domain JSONP requests.
window.MyCallback = function (data) {
console.log(data);
};
function instagramAjax () {
$.ajax ({
url: "url that works",
dataType: "jsonp",
crossDomain: true,
jsonpCallback: 'MyCallback',
success: function(returnedData) {
//my success code that works
},//end success
error: function (xhr, status, err) {
console.log(status, err);
}
});//end ajax request
}//end instagramAjax function
or you can do it:
function instagramAjax () {
$.ajax ({
url: "url that works",
dataType: "jsonp",
crossDomain: true,
jsonp: 'MyCallback',
success: function(returnedData) {
//my success code that works
},//end success
error: function (xhr, status, err) {
console.log(status, err);
}
});//end ajax request
}//end instagramAjax function

How can I get response header using AJAX

I'm trying to get response header using AJAX, but it doesn't work. Apparently I did't do it right:
$.ajax({
type: 'GET',
url:'http://www.somesite.com/',
data: formData,
success: function(data, textStatus, XMLHttpRequest){
alert(XMLHttpRequest.getResponseHeader('some_header'));
}
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.getResponseHeader('some_header'));
}
});
I also try this one:
$.ajax({
type: "GET",
url: 'http://www.somesite.com/',
complete: function(xhr) {
alert(xhr.getAllResponseHeaders());
}
});
Any help?
You are missing a comma.
success: function(data, textStatus, XMLHttpRequest){
alert(XMLHttpRequest.getResponseHeader('some_header'));
}, // <-------------

Why isn't the JSONP callback function getting invoked?

I am trying to use jsonp to access the data at:
https://github.com/users/jbranchaud/contributions_calendar_data
However, none of the solutions I have tried are resulting in either the callback or the success function getting invoked. When I use Chrome/Firefox inspection tools, I can look at the script and see that the response was 200 Ok and that the response text contains the data from the above URL. Nevertheless, neither the callback function nor the success function get called at any point. Any ideas about how to get this to work?
Here is my most recent attempt at getting this to run:
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function parseResults(results) {
console.log('hello function callback.');
}
$.ajax({
url: 'https://github.com/users/jbranchaud/contributions_calendar_data',
type: 'post',
dataType: 'jsonp',
jsonp: true,
jsonpCallback: 'parseResults',
success: function(data, textStatus, jqXHR) {
console.log('success_function');
console.log(data);
},
error: function() {
console.log('error with jsonp request');
}
});
</script>
When I load the page, I see the 'error with jsonp request' in the console, so there is an error with the ajax request. Ideas of why this request isn't succeeding?
In the ajax request, try to set the attribute 'jsonp' to false (jsonp: false).
Basically, JQuery generate an automatic function callback name, like this : JQuery1223...34.
In your case, you ,already, explicitly set the jsonpCallback function name. so you have to put jsonp attribut to false.
your code should be like this :
$.ajax({
url: 'https://github.com/users/jbranchaud/contributions_calendar_data',
type: 'post',
dataType: 'jsonp',
jsonp: false,
jsonpCallback: 'parseResults',
success: function(data, textStatus, jqXHR) {
console.log('success_function');
console.log(data);
},
error: function() {
console.log('error with jsonp request');
}
});

Resources