How to send JSON data to a java function from angularJS - javascript

I have a json like
var rules=[
{
"ruleId": "1234",
"version": "R1234",
"name": "Usage crossing 50MB",
"description": "Find usage more than 50mb",
"active": true,
"created_by": "Rahul",
"create_date": "2015-08-18",
"modified_date": "2015-08-22",
"modified_by": "Subho",
"category": {
"name": "High Usages Rule",
"id": "26"
}
]
Now I want to call a java page or function where i want to get these data so i put it on DB.
this is my controller
(function(){
"use strict";
angular
.module("RulesEngine")
.controller("RulesEditCtrl",
["rules","$state",RulesEditCtrl]);
function RulesEditCtrl(rules, $state){
var vm=this;
vm.rules=rules;
vm.submit=function(){
vm.rules.$save(function (data) {
toastr.success("Save Successful");
vm.ruleJson=data;
});
console.log(vm.ruleJson);
//in yhis vm.ruleJSON holds the the updated json. it prints fine in console.log
}
}
}());
I want to call a function using $ajax or $http.get() to call a function insertEntry() which is in a java page named ShowJson.java. please help me to call the function and send the data.
I tried
$.ajax({
url : "insertEntry?rule="+vm.ruleJson,
type: 'POST',
dataType : "json",
success : function(data) {
console.log("inserted");
},error : function(data) {
}
});
But its not working...

Related

How to generate a POST request

How to write the underlying code given in the Moxtra API in jQuery or JavaScript
POST /v1/UiaduESWsbzFoK9TOldC6zF/binders
{
"name": "My First Binder"
}
{
"code": "RESPONSE_SUCCESS",
"data": {
"id": "B7U3ze39oO08PYO70973lX6",
"name": "My First Binder",
"revision": 3,
"created_time": 1342813061602,
"updated_time": 1342813061602
}
}
Assuming you want to POST the first object and receive the second object as a successful response, here's how you'd do it with jQuery's shorthand AJAX POST function:
var data = {"name": "My First Binder"};
$.post("/v1/UiaduESWsbzFoK9TOldC6zF/binders", data, function(response) {
// Response object contains your response from the server
console.log(response);
});

Correct way to write ColdFusion service to send json data and catch by angularjs

I'm Very stuck in this case when working all day :(. please help me.
I have website need using $http provider to call ColdFusion file from other domain.
let's say this is link to cfm file : http://xample.com/getStockCodesTest.cfm
this is may sample ColdFusion file:
<cfcontent
type="application/json"
/>
{
"items": [
{
"StockCode" : { "col": "Stock Code", "value": "0231" },
"Qty" : { "col": "Qty", "value": "DS" },
"QtyOn" : { "col": "Qty On", "value": "Branch" },
"QtyVal" : { "col": "Qty Val", "value": "200" },
"ReleasedDate" : { "col": "Released Date", "value": "0" },
"S" : { "col": "S", "value": "0" },
"Description" : { "col": "Description", "value": "adfasdf" },
}
]
}
this is angular code (i use coffeescript to write)
ajax = $http {
url: 'http://xample.com/getStockCodesTest.cfm'
method: 'JSONP'
params: {
callback: "JSON_CALLBACK"
}
}
ajax.success (data, status, headers, config) ->
console.log data
but when check firebug. it always return error:
so how can i have a correct way to write a test ColdFusion with content json like that and use angularjs to catch in to my web :(
I'm not exactly sure what you are asking. But I can tell you that you are getting the error because you have invalid JSON.
That comma, right there, remove it. It makes for invalid JSON.
You have to wrap your JSON with the input function name, when a JSONP call is made from AngularJS. That is the reason you see the error.
Example of JSONP response:
/xxx.cfm?_jsonp=angular.callbacks._0 --- request
//expected response is like this - a function from your coldfusion
angular.callbacks._0([{"StockCode" : { "col":...
Then it will work.

JSONP Object Error

I am trying to make a cross domain request to send/recieve some data. I can't get past the object error. Before I was getting the No Transport Error but adding Query.support.cors = true; solved this issue.
var url = "http://CROSSDOMAINSERVER:PORT/Service1.svc/GetDataUsingDataContract";
$.ajax({
type: 'GET',
url: url,
data: 'tool=1&product=Some%20Product&details=9&bogus=should%20not%20pass%20validation',
datatype: "jsonp",
contentType: "application/json",
success: function (response) {
alert(response.data);
},
error: function (error) {
alert(error.statusText);
}
});
If I type the url out in a browser:
http://CROSSDOMAINSERVER:PORT/Service1.svc/GetDataUsingDataContract?&tool=1&product=Some%20Product&details=9&bogus=should%20not%20pass%20validation
I get the correct response.
{"d":{"__type":"ClientSideData:#ProdResourceToolService","details":"9","product":"Some Product","result":"1","site":"Somewhere, SD","systime":"2\/6\/2013 2:50:20 PM","team":"0000000000","tool":"1","user":"username"}}
When I use ajax it does not submit it to the database or return data, just object error. Does anyone have any suggestions on how to get around this?
I should also specify if I remove http://CROSSDOMAINSERVER:PORT/ from var url when debugging locally I get the correct json response. Why does cross domain not work?
This is your current response:
{
"d": {
"__type": "ClientSideData:#ProdResourceToolService",
"details": "9",
"product": "Some Product",
"result": "1",
"site": "Somewhere, SD",
"systime": "2/6/2013 2:50:20 PM",
"team": "0000000000",
"tool": "1",
"user": "username"
}
}
This is a valid JSON string. However, its not valid JSONP. If possible, make your service wrap the json in a function:
responseFunc({
"d": {
"__type": "ClientSideData:#ProdResourceToolService",
"details": "9",
"product": "Some Product",
"result": "1",
"site": "Somewhere, SD",
"systime": "2/6/2013 2:50:20 PM",
"team": "0000000000",
"tool": "1",
"user": "username"
}
});
And in your $.ajax() call, add a property: jsonpCallback: 'responseFunc'.
Also, I would suggest passing the data as an object:
data: { tool: 1, product: 'Some Product' } //etc...
If you can access the service from the serverside without any issues you could create a httphandler (.ashx for example) and let it generate the JSON for you. Then you wouldn't have to deal with the cross domain issues on the client side.

notification update when receive ajax respone

I have method to update notification and when update it return number of notification and content of notification with json type
{
"friend_request": 4,
"request": [{
"user_id": "1",
"picture": "/home/sepdau/",
"name": "Le Chanh"},
{
"user_id": "2",
"picture": "",
"name": "Yii PHP"},
{
"user_id": "4",
"picture": "13366396884.jpg",
"name": "Minh Le"},
{
"user_id": "11",
"picture": "",
"name": "Thang Phan"}]
}​
When I receive I update number of notification success
function updateNotification(){
$.ajax({
url: '/nevergiveup/index.php/site/updatenotification',
type: "post",
dataType: "json",
success: function(data){
if(data.friend_request>0){
$(".zingcounter").text(data.friend_request); //update number of nofitcation
// load the template file, then render it with data
var html = new EJS({url: '/nevergiveup/jstemplates/friend_request.ejs'}).render(data);
//$("#frlist").append(html);
//$(html).replaceAll('#replacehere');
$('#replacehere').replaceWith(html); // update content of notification
}
setTimeout(updateNotification,10000);
},
error: function(){
setTimeout(updateNotification,10000);
}
});
}
I use EJS to build a template of content
I have a <div id="replacehere"> to replace my content here
I use $('#replacehere').replaceWith(html); to replace but it success when first request in 10s after
I see json data receive has a new content and number of notification has change but content not change.
How to change it when receive new content.
Guess you need just use
$('#replacehere').html(html);
otherwise it will remove #replacehere div... and second request wont find it to put content in...

django json loop

How to i get the values of particular field for Example: son, flower(Title) from
json = [{"pk": 1, "model": "book", "fields": {"title": "sum", "author": "son", "description": "dfg"}}, {"pk": 2, "model": "book", "fields": {"title": "Eradication", "author": "flower", "description": "nandhu"}}]
using ajax in ajax success part. Can anyone help me ?
With jQuery, you can just use the getJSON function as such:
$.getJSON('{% url your_json_view %}', function(data) {
for(var i=0; i<data.length; i++) {
if (data[i].fields.author == 'flower') {
alert('I got the book by flower');
}
}
});
Just to make sure, here's the python view that goes with it:
def your_json_view(request):
# ... build your data
return http.HttpResponse(simplejson.dumps(data))
The url should be defined nicely with a name too:
url(r'/your/json/view/', 'your_json_view', name='your_json_view')

Categories

Resources