web service call with more than one parameter - web-services

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/

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"/>

calling datasnap webservice from javascript into variable

I have created a Datasnap REST server.
A ReverseString method gets created by default that simply reverses the string passed to the method.
When I invoke the method using the browser URL:
http://dummydomain.com:81/datasnap/rest/TServerMethods1/ReverseString/ABC
I receive {"result":["CBA"]} as result. (server runs on port 81 and the parameter passed to the method is ABC)
I have tried the following code to get the result into a variable in javascript, but without success:
var options={
type: "GET",
url: "http://dummydomain.com:81/datasnap/rest/TServerMethods1/SayHello",
data: "ABC",
dataType: "json",
success: function(data) {
var json = JSON.stringify(data);
$("p").after('<div id="data1">Your result is: ' + json.result + '</div>');
}
}
$.ajax(options);
What am I doing wrong? In the end I need 'CBA' in a variable. If there are some good resources out there, please advise. I have been googling but everybody seem to do it in their own way.

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

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

What is the best way to send multiple parameters to a webservice from a client, which is a scripting language?

I would appreciate if someone can point me the recommended ways of passing multiple (say 10) parameters from client to a webservice. I have the requirements for the server side, so once I figure out the best way to pass data, I can expose the webmethod on the server side (.NET). FYI, I have a SOAP client that is implemented in Javascript. Thanks in advance!
JQuery:
function AddLocation(ParentID, name, name2, code) {
$.ajax({
type: "POST",
url: "../server.asmx/Save",
data: "{'ID':'0','ParentID':'" + ParentID + "','Name':'" + name + "','Name2':'" + name2 + "','Code':'" + code + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var result = data.d;
//more code here
}
});
}

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