Solved

convert ajax xmlhttprequest to jquery ajax

Posted on 2013-05-17
4
977 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now