Solved

convert ajax xmlhttprequest to jquery ajax

Posted on 2013-05-17
4
1,012 Views
Last Modified: 2013-05-18
Hi, I am trying to convert the following code to jquery, but I am having issues.  This uses cross site scripting, but I changed the setting on my browser.  I can successfully use the non jquery code.

      Ajax=new XMLHttpRequest();
        Ajax.open('POST', url, true);
      Ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      Ajax.send(data);

Here is what I currently have:
jQuery.ajax({
      url: loginUrl,
      contentType: "text/html; charset=Windows-1256",
      dataType: "JSONP",
      async: true,
      success: function (responseText, status) {
            alert("success" + responseText);
      },
      error: function (xhr, ajaxOptions, thrownError) {
            responseText("error=" + xhr.status + "=" + thrownError);
      }
0
Comment
Question by:jackjohnson44
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39176412
Try this :
$.post(loginUrl, function(responseText) { alert("success" + responseText); });
0
 

Author Comment

by:jackjohnson44
ID: 39176423
Actually it might be working better than I thought.  It is not hitting my success area, but if I look in fiddler I can see a 200 response in the header, and the xml response in the text view.  For some reason, everything is working, but my error area is still being hit.

This is what I have:
var postData = {};
postData["source"] = source;
var loginUrl = url + "LogIn?source=" + source + "&version=" + version;
jQuery.ajax({
      url: loginUrl,
      //dataType: "xml",
      data: postData,
      async: true,
      type: "POST",
      success: function (responseText, status) {
            alert("ASDF");
      },
      error: function (xhr, ajaxOptions, thrownError) {
            responseText("xhr.status=" + xhr.status + "\najaxOptions=" + JSON.stringify(ajaxOptions) + "\nthrownError=" + thrownError);
      }
});
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39176479
please provide a link to a simple page which reside on your site
0
 

Author Closing Comment

by:jackjohnson44
ID: 39177702
Your answer will work.  I had an issue due to cross site scripting.  Thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
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…

786 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