Solved

convert ajax xmlhttprequest to jquery ajax

Posted on 2013-05-17
4
1,028 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 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)

840 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