Solved

Trouble with JQuery Ajax / ASP.Net

Posted on 2010-11-22
3
429 Views
Last Modified: 2012-08-13
Experts,

I'm having some trouble getting some ajax functionality to work in all browsers.  The code IS working correctly in IE8 and Firefox.  However, it is not working in Chrome, Safari, or Opera.

Below is my JavaScript (i.e. using jquery-1.4.3):
 
var ResponseTimer = "";

$(document).ajaxError(function (e, xhr, settings, exception) {
    alert('error in: ' + settings.url + ' \nerror: ' + exception + '\nxhr status: ' + xhr.status);
});

function StartResponseTimer()
{
    alert('CIP Settings have been sent. Awaiting response from control.');

    CheckForControlRepsonse();

    //Start javascript poll timer:
    //ResponseTimer = setInterval('CheckForControlRepsonse()', 2000);
}

function CheckForControlRepsonse()
{
    $.ajax
    ({
        type: 'GET',
        async: true,
        url: 'http://localhost:52971/AjaxResponder.aspx?CallingPage=CIP',
        dataType: 'html',
        timeout: 0,
        success: function (responseData) { ProcessAjaxResponse(responseData); }
    });
}

function ProcessAjaxResponse(responseData)
{
    if (responseData != 'no-response')
    {
        //If ACK or NCK received then stop timer:
        //clearInterval(ResponseTimer);

        //Display messages to user:
        var message = '';
        if (responseData == 'ACK') message = 'CIP Settings Received By Control.';
        if (responseData == 'NCK') message = 'An error occured when atempting to send CIP\r\n settings to control. Please try again.';
        alert(message);
    }
}

Open in new window


And here is my server side code that is handling the ajax request:
 
namespace XXXXXXXXXX
{
    public partial class AjaxResponder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string CallingPage = Request.QueryString["CallingPage"].ToString();

            switch (CallingPage)
            {
                case "HOME": HomeResponder(); break;
                case "CIP": CIPResponder(); break;
            }
        }

        private void HomeResponder()
        {
            //....
        }

        private void CIPResponder()
        {
            string response = "";

            //ToDo: Get response message from listener server.

            //Temp:
            response = "ACK";

            if (response != "") Response.Write(response);
            else Response.Write("no-response");
        }
    }
}

Open in new window


When I run the site in IE8 or Firefox I get a javascript popup saying: "CIP Settings Received By Control." Which is just what I expect to get.

When I run the site in Chrome or Safari, I get a popup showing:

    Error in: http://localhost:52971/AjaxResponder.aspx?CallingPage=CIP
    error: undefined
    xhr status: 0

Which would be the ajaxError() handler being executed.

When I run the site in Opera, I get nothing.....

Please advise as to what I'm not doing correctly.  Thanks.
0
Comment
Question by:P1ST0LPETE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
JosephEricDavis earned 500 total points
ID: 34192598
Try changing your dataType to text instead of html
0
 
LVL 10

Author Comment

by:P1ST0LPETE
ID: 34193016
Tried changing the dataType to text.  Results didn't change a bit.
0
 
LVL 10

Author Comment

by:P1ST0LPETE
ID: 34198264
Figured out the issue on my own.  Button I was using to kick off the javascript was also kicking off a standard asp postback of it's own.  This was causing the XmlHttpResponse.status attribute to = 0, and fouling up the ajax all together.  Separated the code out so things weren't happening at the same time, and everything works.

Thanks for the help though Joseph.
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question