Getting error when calling generic handler in javascript - handler

function callFabricsHandler(type, param, reqType) {
var ans = null;
var url = "http://localhost/FabricsShop/fabricsHandler.ashx?reqType=" + reqType + "&" + param;
$.ajax({
url: url,
type: 'GET',
data: reqType,
dataType: 'json',
success: function (data) {
ans = data;
alert(ans);
},
error: function (jqXHR, status, err) {
alert( err+'\n'+jqXHR+'\n'+status);
}
});
return ans;
}
Hi, This is my code I am calling a handler from my javascript file ,when I am calling this always getting following message:
syntaxerror:Unexpected token H in JSON at position 0 [Object Object]
parsererror
This is alert of error part
Can anyone please help me.
Thank you

Related

How to pass Client Side data to Server Side using Ember.js

I'm studying Ember.js myself and I'm stuck with a problem I'm creating a sample app and I need to send the client side values to Server Side but I dont know how to do that I know the traditional way like the below code
function create() {
var data = {
'EmailID': $('#emailid').val(),
'password': $('#password').val()
}
$.ajax({
url: '/EmberNew/Home/Create',
type: 'POST',
data:data,
success: function (response) {
alert("hi");
}
});
return false;
}
but In Ember i dont Know How to do that my current code is given below
//Application
App = Em.Application.create();
//Model
App.Users = Em.Object.extend({
name: null,
password:null
});
//View
App.UserTextField = Em.TextField.extend({
insertNew: function () {
App.alertController.alertDetails();
}
});
App.PassTextField = Em.TextField.extend({
insertNew: function () {
App.alertController.alertDetails();
}
});
//controller
App.AlertController = Em.ObjectController.extend({
content: [],
username: '',
password: '',
alertDetails: function () {
var me = this;
var username = me.get("username");
var password = me.get("password");
alert('The User Name Is' + 'username' + 'And Password Is' + 'password');
}
});
App.alertController = App.AlertController.create();
I got the textbox values from alertDetails function and how can I pass them to server side
App.Record = Ember.Object.extend({
name: '',
other: ''
}).reopenClass({
records: [],
find: function() {
var self = this;
$.ajax({
url: "/api/records/",
type: "GET",
cache: false,
dataType: "json",
beforeSend: function() {
//if you want to call this often and need to clear + reload it
return self.records.clear();
},
success: function(results) {
var result;
for (_i = 0, _len = results.length; _i < _len; _i++) {
result = results[_i];
self.records.push(self.records.addObject(App.Record.create(result)));
}
},
error: function() {
return alert("error: failed to load the records");
}
});
return this.records;
}
});
Now that you have your model setup, you can call it from your route model hook
App.RecordsRoute = Ember.Route.extend({
model: function() {
return App.Record.find();
}
});
The find method returns an empty array right away, your template is then bound to it. When the ajax call returns w/ success and you update that array the handlebars template will update it for you w/out any DOM or jQuery glue code

Not able to change the value of a variable inside the $ajax success

I am having a curiously weird problem, here is my JS code
function ValidateUser(username) {
var userExists = "somevalue";
$.ajax({
contentType: 'application/json, charset=utf-8',
type: "POST",
url: "/Controller/Validate",
data: JSON.stringify({ username: username }),
cache: false,
dataType: "json",
success: function (response) {
alert(response);
if (response == true) {
userExists = true;
alert("user exists: " + userExists);
} else {
alert("user exists: " + userExists);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('error');
}
});
return userExists;
}
well, the action works fine and validates user correctly, but the function ALWAYS returns "somevalue".....What gives? My hunch is that it has something to do with a function scope. But how do I resolve this issue.
Because the function you're calling $.ajax from returns before the Ajax call completes.
The simple answer is to do the work you need to do in the success function, either with an immediate function (as now), or by setting success to an existing function.
You could also use jQuery's $.when function.
You can use the setting "asynch: false" too, it will lock the browser, disabling any actions while the request is active. Here is your code changed:
function ValidateUser(username) {
var userExists = "somevalue";
$.ajax({
contentType: 'application/json, charset=utf-8',
type: "POST",
url: "/Controller/Validate",
asynch: false,
data: JSON.stringify({ username: username }),
cache: false,
dataType: "json",
success: function (response) {
alert(response);
if (response == true) {
userExists = true;
alert("user exists: " + userExists);
} else {
alert("user exists: " + userExists);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('error');
}
});
return userExists;
}
You can get more information here: jQuery.ajax()

what's wrong when using jquery to call wcf service, always return internal server error?

Below is my jquery codes, and it always return internal server error, and i'm sure that my wcf service can be called from i.e and iis.
$.support.cors = true;
$.ajax({
type: "post",
url:"http://localhost:8080/WcfService/FunHeartWork.svc/UpdateMedia",
data: '{"desc":"' + text + '","albumId":"'+albumId+'","mediaId":"'+mediaId+'","userName":"'+userName+'"}',
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (result) {
alert(result);
},
error:function(a,b,c){alert(c);},
failure: function (msg) {
alert(msg);
}
});
Try changing the data line to:
data: {
desc: text,
albumId: albumId,
mediaId: mediaId,
userName: userName
},

how to call this calling WCF method continuously

I have an ajax enabled WCF service method :
[OperationContract]
public string Test(string name)
{ return "testing testing." + name; }
and I am calling it with following code:
$(document).ready(function () {
var varData = $("#NewSkill").val();
$("#Button1").click(function () {
$.ajax({
type: "POST",
url: "TimeService.svc/Test",
data: '{"name" : "John"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
}
});
});
});
I want to call this method continuously after every 5 seconds using above code . How can I do this ?
Move the $.ajax(); part to a Javascript function say AjaxCall(). Create a javascript variable
var isActivated = false;
$(document).ready(function () {
while(isActivated){
setTimeout("AjaxCall()",3000);
}
}
);
$("#Button1").click(isActivated = true)
Hope this helsps...

Variable visibility in callbacks functions

I did this function for get results of a query directly in an useful datastructure. The problem is the follow: in the first console.log() call , inside the callback function, the stored_data var contains the exact results, in the second console.log() call the stored_data variable looks like not initialized. Suggestions??
Below the code:
function dojo_query_mysql(query_string) {
//The parameters to pass to xhrPost, the message, and the url to send it to
//Also, how to handle the return and callbacks.
var stored_data;
var raw_data = new Object;
var xhrArgs = {
url: "query.php",
postData: query_string,
handleAs: "text",
load: function(data) {
raw_data = dojo.fromJson(data);
stored_data = new dojo.data.ItemFileReadStore({data: raw_data});
console.log(stored_data);
},
error: function(error) {
//We'll 404 in the demo, but that's okay. We don't have a 'postIt' service on the
//docs server.
//stored_data = error;
}
}
//Call the asynchronous xhrPost
var deferred = dojo.xhrPost(xhrArgs);
console.log(stored_data);
return stored_data;
}
I have just remembered that the function doesn't wait the end of the callback execution, for wait the callback end just do a little change to the code:
var xhrArgs = {
url: "query.php",
sync: true, // THIS IS FORCE THE SYNCRONIZATION BETWEEN THE CALLBACK AND THE CODE
postData: query_string,
handleAs: "text",
load: function(data) {
raw_data = dojo.fromJson(data);
stored_data = new dojo.data.ItemFileReadStore({data: raw_data});
console.log(stored_data);
},
error: function(error) {
//We'll 404 in the demo, but that's okay. We don't have a 'postIt' service on the
//docs server.
//stored_data = error;
}
}