How to call WCF Web Service (.svc) using jQuery - web-services

Hi I am trying to call a WCF web service(.svc)
$.ajax({
url: 'http://www.somedoamin.com/Services/service.svc',
dataType: "text/xml",
contentType:'text/xml; charset=utf-8',
beforeSend: function(xhr){
// Pass the soap action onto the proxy.
xhr.setRequestHeader(
"SOAPAction","http://www.anotherdomain.com/Services/Login"
);
},
data:soapMessage,
type: 'POST',
success: function(res) {
var myXML = res.responseText;
console.log('Response ',myXML);
},
error:function(jqXHR, textStatus, errorThrown,exception) {
console.log('An error occured ');
}
});
I am getting error callback. "An error occured"
So, Should i need to add some parameters to WCF Service ?

Refer this existing How Do I call A WCF Web Service from jQuery?
This is the solution they given
http://www.west-wind.com/weblog/posts/2008/Apr/21/jQuery-AJAX-calls-to-a-WCF-REST-Service

Related

Create Item by using Item web api

I want to know how implement javascript method to create new item by using sitecore item web api.I am trying for below code my self.
But in the browser console show this error:
XMLHttpRequest cannot load http://myproject/-/item/v1/sitecore/Content/Home?name=MyItem5&template=Sample/Sample%20Item&sc_database=master. Request header field X-Scitemwebapi-Password is not allowed by Access-Control-Allow-Headers.
Please can anyone help me!!!!!!!
function createItem(){
jQuery.ajax({
crossDomain: 'true',
type: 'POST',
url: 'http://myproject/-/item/v1/sitecore/Content/Home?name=MyItem5&template=Sample/Sample Item&sc_database=master',
dataType: 'JSON',
contentType: 'application/x-www-form-urlencoded',
headers:{
"X-Scitemwebapi-Username":"sitecore\\Admin",
"X-Scitemwebapi-Password":"b",
},
success: function(data) {
alert(JSON.stringify(data));
},
error: function(res, error){
alert(JSON.stringify(res))
alert(res+ ' something is wrong');
}
});
}
Make sure you have these settings in your Sitecore.ItemWebApi.config
itemwebapi.mode="StandardSecurity"
itemwebapi.allowanonymousaccess="false"/>

AngularJS $http provider not passing cookies to .NET Web Service

I am having problems using the $http provider in AngularJS to make an API call to a .NET backend that requires Authentication. The .NET backend service expects the ".ASPXAUTH" and "__RequestVerificationToken" cookies to be included in the call.
When I use plain JS and .ajax, passing the cookies in the beforesend block works.
$.ajax({
url: SERVER_URL + '/api/accounts',
type: 'GET',
dataType: 'json',
crossDomain: true,
xhrFields: {
withCredentials: true
},
success: function(data, status){
// ...
},
error: function (request, textStatus, errorThrown) {
// ...
},
beforeSend: function (req) {
req.setRequestHeader("Cookie", REQUEST_VER_TOKEN);
req.setRequestHeader("Cookie", ASPXAUTH_TOKEN);
}
});
In AngularJS, I've tried setting it in the $httpProvider and in the $http call without success:
(in the config block):
// ...
app.config(function ($routeProvider, $httpProvider) {
$httpProvider.defaults.headers.common[".ASPXAUTH"] = '....';
$httpProvider.defaults.headers.common["__RequestVerificationToken"] = '....';
$httpProvider.defaults.useXDomain = true;
// ...
(in the $http call):
$http({ url: 'http://SERVER_URL/api/accounts', method: 'GET',
{headers: {
'__RequestVerificationToken': '....',
'.ASPXAUTH': '....'
}}
})
.success(function(data, status, headers, config) {
// ...
})
.error(function (data, status, headers, config) {
// ...
});
I get a 401 Unauthorised error. If the Authorization block is removed from the server code, the code works without needing to insert the headers. How do I emulate the same beforesend block in AngularJS and ensure that the cookies are passed to the server during the API call?
Did you try to pass your header parameters not in a json object but directly into the headers attribute?
$http({ url: 'http://SERVER_URL/api/accounts', method: 'GET',
headers: {
'__RequestVerificationToken': '....',
'.ASPXAUTH': '....'
}
}).success(function(data, status, headers, config) {
// ...
})
.error(function (data, status, headers, config) {
// ...
});
Hope it can help.

call ASP.net 1.1 service in asp.net using Jquery

I am trying to call ASP.net 1.1 service in asp.net using Jquery, can't figure put what I am doing wrong here, Pleaseeeee help
Javascript Method
function YesCheckChanged(vSRID) {
var para = {
SRID: vSRID
};
jQuery.ajax({
type: "POST",
url: serviceUrl + "/YesRdoClicked",
dataType: "xml",
data: para,
contentType: "application/xml; charset=utf-8",
success: function(data, status) {
alert(data);
edata = $(data).find("string").text();
alert(edata);
},
error: function(e, status) {
alert(status);
}
});
}​
Error
System.InvalidOperationException: Request format is invalid:
application/xml; charset=utf-8.
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
Service method Code
[WebMethod(true)]
public string YesRdoClicked(int SRID)
{
clsEntity obj = new clsEntity();
obj.New_DeleteTempEntity(SRID);
return "yes";
}
http://www.codeproject.com/Articles/1231/ASP-NET-Web-Service
I finally resorted to native Activex calls as no other way seem to work for me. Personally I wanted to avoid using it. But It works well.
A word of caution here, activeX are not very well supported in browsers other than IE so you need to either have IE Only application or convince the client to upgrade to higher version of asp.net.

web service call with more than one parameter

In my app, I am calling .Net soap based webservices.
My web service call function is :
function CallService5() {
$.ajax({
type: "POST",
url: "http://10.0.2.2:51434/Service1.asmx/GetAllTableStatus",
dataType: "json",
data: "{}",
contentType: "application/json; charset=utf-8",
success: OnSuccess,
error: OnError
});
}
How can i add more than one parameter..?
I think you can use like this
var firstName = document.getElementById("txtFirstName").value;
var lastName = document.getElementById("txtLastName").value;
data : "{'firstName':firstName,'lastName':lastName}"
You can add parameters as query string in the url part of service
"http://10.0.2.2:51434/Service1.asmx/GetAllTableStatus?para =val"
You read the following articles:
it gives good solutions..
http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/
http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

Using jquery $.get to call an external web service

I am calling the following jQuery code on page load to test the concept of calling an external web service from the client. The data object in the success callback is always empty. What am I doing wrong?
$.ajax({
url: "http://www.google.com/search",
type: 'GET',
data: { q: "green tea" },
success: function(data) { alert("Data Loaded: " + data) },
dataType: "text/html"
});
It's the same-origin policy you're hitting here, it's specifically in place to prevent cross-domain calls for security reasons. The expected behavior is for the response to be empty here.
You either need to fetch the data via JSONP or get the data via your own domain, your server proxying the request.
It's worth noting Google has a full JavaScript API for searching that you may want to check out for doing this.
browser dont allow you to make cross domain request(a security feature). there is a hack for that with a limitation that you can get only json as response.
----the trick (hack)----
using jquery(or javascript)you create a new script tag and with src="url_of_third_party?", when that request is made you get json from cross site.
jQuery('body').append('<script src="cross_site_url" type="text/javascript"></script>');
or simply you can do this
$.ajax({
url: "http://www.google.com/search",
type: 'GET',
data: { q: "green tea" },
success: function(data) { alert("Data Loaded: " + data) },
dataType: "jsonp",
});
note: dataType=jsonp