Passing html throgh ajax to wcf service - ajax

I want to pass html content for example the body content to a wcf service through ajax call, its saying URL is too long. Can this be solved

Assign html content in javascript variable and use below code.
var html_content= "<html...content>";
$.ajax({
url: "ajax.php",//file_name
type: "POST",
data: {
content: html_content
},
});

Related

Passing API.config key to ajax call's url setting

I have several views where I'm passing values to an external web service via an ajax post when the user clicks on submit or other action. For the url section of the ajax call I'm trying to pass a key from API.config, located in the Solution Items folder, where the value is different in each build/environment -
Solution Items folder layout
Key I'd like to pass to the ajax url: (API.config)
<appSettings>
<add key="RestApiEndpointBaseUrl" value="http://devserver/api/" xdt:Locator="Match(key)" xdt:Transform="SetAttributes"/>
Script within the view: (index.cshtml)
<script type="text/javascript">
var BaseUrl = '#System.Configuration.ConfigurationManager.AppSettings["RestApiEndpointBaseUrl"]';
$('#login').click(function () {
var note = {"Account":"#Model.AccountNumber","Text":"Log In","ActionCode":"CW","ResultCode":"LI","SequenceNumber":1,"UserId":"WEB"};
$.ajax({
type: "POST",
data: JSON.stringify(note),
url: BaseUrl + "Note",
contentType: "application/json"
});
});
</script>
Also, if I add "ToString()" to the end of the variable declaration like this -
var BaseUrl = '#System.Configuration.ConfigurationManager.AppSettings["RestApiEndpointBaseUrl"].ToString()';
I get a NullReferenceException error.
Otherwise, I get an empty string for the url. I'm not sure what I'm missing or doing wrong.
How, or Can I properly pass this as the url of the ajax call?

AngularJS http POST with $scope

I am trying to do a http post using AngularJS but angular is not converting my $scope variable to JSON.
Here is my code:
$http({
method: "POST",
url: "/Account/Login",
data: $scope
})
Which results in the request POST message having
"$SCOPE"
but if I change it to output any of my scope properties, it is sending the message with correct properties, e.g.:
$http({
method: "POST",
url: "/Account/Login",
data: { email: $scope.email, password: $scope.password }
})
Which results in the request POST message having
{"email":"asdasd#Asdasd.asd","password":"asd"}
Do I always have to wrap my requests like this? Or is there a way to tell AngularJS to send all properties on scope? Any Pro's / Con's?
Sending the $scope is not a good idea, It contains lot more than your email and password
You should create a property like $scope.user and then attach the model to it like $scope.user.email. Now you can send it using $scope.user
$http({
method: "POST",
url: "/Account/Login",
data: $scope.user
})
a lil about $scope
scope is an "object" that "binds" to DOM element where you apply controller. All child elements can read and modify scope data (unless you modify primitives in new scopes or they're isolated
for more official doc is they way

Attach requestbody to jQuery ajax

I am trying to attach data to my requestbody while sendign using jQuery ajax.
If I tried to do it using the extension RESTCLient is either firefox or chrome it works fine, which means that my method on the serverside is working fine.
That is why I am pretty sure that it the ajax call I am making
$.ajax({
url: 'lingosnacks/delete/'+ id,
type: 'POST',
data: $('#email').val() + $('#password').val()
dataType: "json",
success: function(data) {
console.log("FILL| Sucess| ");
console.log("FILL| Sucess| Data| " + data);
fill(data);
}
});
The data line is wrong, it should be very similar to a JSON string, like this:
data: {email: $('#email').val(), password: $('#password').val()},
You need to have the data you are sending in this format:
email=blah%40blah.com&password=pass123
You can do that with jQuery using $('form').serialize()
Also, you are missing a , after your data string in the Ajax call.
Actually data param in jQuery Ajax method is for sending url params.
You can send the same by appending then into the url but to make the code more readable e and organized i would prefer to use data variable.
So your data content should look like :
data : "email="+$('#email').val()+"&password="+$('#password').val();
I am not pretty sure if sending params like a json object will work or not because i never used it.

Grails: Passing a javascript variable to a template

I am new to ajax so maybe this is obvious. I've tried many different not-working approaches. I have javascript that when you click on a button:
an ajax call that grabs some data from a controller - returns an object
display that data in a template that I will show on the page
Here is the javascript/ajax:
<script type="text/javascript">
$("#show").click(function () {
$.ajax({ url: '/Myproject/result/index',
type: "POST",
data: { id: id},
success: function(result) {
alert("Success:" + result); // Can see getting object back.
}});
$(".resulttable").show();
});
Here is the key line in grails view template:
<g:each in="${allResults}" status="i" var="result">
How do I get the data from the javascript to the gsp code (ie allResults)?
Do I have to "refresh" this template to display new data?
thanks.
You just can't make your javascript/jquery code populate things in the gsp, since the gsp is processed server-side and javascript is processed client-side, after the gsp rendered all html documents and populated them with your model. You need to be aware that your page was already processed, so things like ${variables} won't be reloaded anymore. So when you do this:
$(".resulttable").show();
It's not gonna show the result you're waiting for. You have to use javascript code to reload your result table.
So if you're using ajax here, you should use the function success to alter your html table via javascript/jquery since you already have the response you wanted. Maybe this read can help you. Oh, and I think it would be better if in your ajax call, you would define a dataType (json works great in your case), like this:
$("#show").click(function () {
$.ajax({ url: '/Myproject/result/index',
type: "POST",
data: { id: id},
dataType: 'json',
success: function(result) {
alert("Success:" + result); // Can see getting object back.
}});
$(".resulttable").show();
});
Just to make clear what kind of response you're getting from the controller.
You do not need to write your ajax calls the hard way. There are some Grails intern tags you can use inside your html:
http://grails.org/doc/latest/ref/Tags/remoteFunction.html
http://grails.org/doc/latest/ref/Tags/submitToRemote.html
http://grails.org/doc/latest/ref/Tags/remoteLink.html
Following the links you will find some nice examples...

How use Facebook Javascript SDK response in Ajax request?

Supposing I have the following code which returns a Javascript object which I can read in Firebug's console:
FB.api('/me',function(apiresponse){
console.log(apiresponse);
});
How can I then use the data from apiresponse in an Ajax request on the same page?
Currently my Ajax request looks as follows:
$.ajax({
// CodeIgniter URL
url: "<?=site_url?>('login/add_fb_users'); ?>",
type: 'POST',
data: apiresponse,
success: function(data) {
alert(data);
}
});
I know very little about Javascript, but reading around the subject leads me to think I have to convert the Javascript object to a JSON string. Is that correct? Am I on the right track?
You could put your AJAX call inside the handler for the API call like below..
FB.api('/me', function(apiresponse){
console.log(apiresponse);
$.ajax({
// CodeIgniter URL
url: "<?=site_url?>('login/add_fb_users'); ?>",
type: 'POST',
data: apiresponse,
success: function(data) {
alert(data);
}
});
});
one possible way:
define a global variable in your javascript, e.g. var myVar1;
set apireponse to the global variable in your FB.api callback (i.e. where u call console.log)
reference the var myVar1 in your ajax fcn.

Resources