ajax failing only in IE - asp.net

i have this jquery code
$("#ProfileBtn").click(function () {
if (true) {
$.ajax({
type: "POST",
url: "sMaster.Master/outClick",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("~");
location.reload();
},
error: function () {
alert("!");
}
});
}
});
that calls the asp.net function:
[ScriptMethod, WebMethod]
public static void outClick()
{
}
when i try it using chrome or firefox everything is fine (alert("~")), but on Internet Explorer it fails (alert("!")).
any idea why..?
update: i've tried putting this instead of the error:
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR);
alert(textStatus);
alert(errorThrown);
}
and what i get in the alerts is:
[object XMLHttpRequest]
error
undefined
i've also tried jqXHR.Status and it gave me 404 in the alert.

As far as I understood the problem is that ajax (maybe only in IE) can't call functions from master page. (I tried moving the jQuery and asp.net function to an aspx and aspx.cs page and it all worked)
Thought I did also find a better way to do what I intended.
Thanks anyway.

Related

Web Method not called in ajax aspx web form

Here is my ajax request:
$("#<% =txtDiagnosisData.ClientID %>").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
url: 'EMR.aspx/SearchDiagnosis',
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (data) {
// response(data.d);
alert("success");
},
error: function (result) {
alert("error");
}
});
}
});
Here is my function:
[WebMethod]
public static List<string> SearchDiagnosis()
{
return new DataAccess().GetDiagnosis();
}
My method is not called from ajax, it always goes to the error part
How to solve this?
There could be an issue that you might be using Session variables inside you web method directly or indirectly.
Somewhere in the function DataAccess().GetDiagnosis();
In that case use attribute like [WebMethod(enableSession: true)] in stead of
[WebMethod]
Also try to get the error by implementing error properly like below
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 500) {
alert('Internal error: ' + jqXHR.responseText);
} else {
alert('Unexpected error.');
}
}
Ref : jQuery ajax error function and see what error exactly you are getting.

Calling ajax request from html page inside asp.net project

I cannot get a simple ajax example to work from an html page inside my visual studio project. It works fine from a webform (aspx):
...
webform1.aspx and form1.html
...
function ShowCurrentTime() {
alert("before json");
$.ajax({
type: "POST",
url: "json.aspx/GetCurrentTime",
data: "{name: bob }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
alert("after json");
}
function OnSuccess(response) {
alert(response.d);
}
...
webform1.aspx and form1.html
...
<input id="btnGetTime" type="button" value="Show Current Time"
onclick="ShowCurrentTime()" />
...
json.aspx
...
[WebMethod]
public static string GetCurrentTime(string name)
{
return "Hello " + name + Environment.NewLine + "The Current Time is: "
+ DateTime.Now.ToString();
}
If I put the code in webform1.aspx, it works fine. If I put it on form1.html, nothing comes back from json.aspx. I am running the project in debug mode from my machine using vs2013. Any help would be appreciated.
Update #1
I checked fiddler and the server is returning the following result (500):
{"Message":"Invalid JSON primitive: bob.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n at ...
Your json is malformed. Strings need to be in quotes. Put bob in quotes and you should be good. I'm unsure why it's working on the ASP.NET page though, unless it has the quotes there.
function ShowCurrentTime() {
alert("before json");
$.ajax({
type: "POST",
url: "json.aspx/GetCurrentTime",
data: "{name: \"bob\" }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
alert("after json");
}
function OnSuccess(response) {
alert(response.d);
}

how to set jquery when my asp.net page want to load

i want to when my page loading , loading with jquery (progress bar) and when finished, user can see page
Asp.Net with c#
In this case, you have to load everything via jquery ajax. And while data is getting loaded, you can show a loading div on the page and hide it when its done.
Sample jquery-ajax function for your reference.
$.ajax({
type: "POST",
url: url,
data: data,
contentType: "application/json; Characterset=utf-8",
dataType: "json",
success: function (data) {
// hide loading div
},
error: function (request, status, error) {
alert("Exception Handling : \n" + request.responseText);
},
complete: function () {
//
}
});

Just another jQuery AJAX not POST parameters correctly

I've googled many similar situations, but none of them could solve my problem. Please take a look at my code:
JavaScript:
$.ajax({
type: 'POST',
url: 'alarmInfo.aspx',
data: {request:'BasicGpaInfo'},
dataType: "json",
success: function (data) {
alert(data);
},
error: function () {
alert("Error in loading alarm information!");
}
});
ASP.NET:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["request"] == "BasicGpaInfo")
{
Response.Write(BasicGpaInfo());
}
else
{
Response.Write("Nothing");
}
}
This always returns "Nothing" and break point tells that Request.Form is null. And I have tried with GET and Request.QueryString which gives the same situation.
I guess there's something wrong with data in ajax function and I've tried with the following things that won't help:
data: $.param({request:'BasicGpaInfo'})
data: "{request:'BasicGpaInfo'}"
data: {request:'BasicGpaInfo'}
It won't work on all Web Browsers.
Please give some advice. Thanks!
I tested your code and it runs fine. However it always returns "Error in loading alarm information!" because you are not returning Json from the server.
Javascript is fine, once you return json, it will go into success.
You are returning the whole page, and your ajax method is getting the whole html instead of Json from BasicGpaInfo()
try putting a breakpoint in alert, and you will see all the data come inside data
error: function (data) {
alert("Error in loading alarm information!");
}
alternatively try
error: function (data) {
alert(data);
}
Here is the full code
$.ajax({
type: 'POST',
url: 'Default.aspx/BasicGpaInfoWebMethod',
data: { request: 'BasicGpaInfo' },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
},
error: function (data) {
alert("Error in loading alarm information!");
//alert(data); // uncomment to see the whole response
}
});
and your webmethod will be :
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string BasicGpaInfoWebMethod(string request)
{
return request;
}
Try this JSON.stringify before post. For using JSON.stringify in IE<=7 include json2.js file from json.org
$.ajax({
type: 'POST',
url: 'alarmInfo.aspx',
data: JSON.stringify({ request: 'BasicGpaInfo'}),
dataType: "json",
success: function (data) {
alert(data);
},
error: function () {
alert("Error in loading alarm information!");
}
});

Weird issue; Button click, Jquery to consume ASMX

I'm having quite a frustrating problem that I'm sure is TRIVIAL. Whenever I move this jQuery post inside the click function it sometimes works, but most of the time does not.
If I move it outside it works and posts and gives a response like it should everytime..
::boggled:: Someone please enlighten me.
$(document).ready(function() {
$('#Button1').click(function() {
//alert(document.getElementById('TextBox1').value);
$.ajax({
type: "POST",
url: "sudoku.asmx/getSolution",
data: "{'pid':'34367'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert("succes " + msg.d);
},
error: function(msg) {
alert("fail " + msg.d);
}
});
});
});
Looks like you need to have your click event return false.
$("#Button1").click(function()
{
$.ajax(...);
return false;
});

Resources