Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show AJAX response message in alert?

Tags:

json

ajax

I am sending username and password as request parameter to the server in AJAX and trying to show the response message. But not able to showing the response message.In fiddler it is showing the response message. But while on the browser screen it is not showing.PLEASE somebody help me out where i am wrong or need to change anything.. I have written like this-

$(document).ready(function () {
  $("#btnCity").click(function () {
    $.ajax({
      type: "POST",
      url: "http://test.xyz.com/login",
      crossDomain: true,
      contentType: "application/json; charset=utf-8",
      data: { username: "abc", password: "1234" },
      dataType: "JSONP",
      jsonpCallback: 'jsonCallback',
      async: false,
      success: function (resdata) {
        alert(resdata);
      },
      error: function (result, status, err) {
        alert(result.responseText);
        alert(status.responseText);
        alert(err.Message);
      }
    });
  });
});
like image 941
ShutterSoul Avatar asked Oct 21 '22 04:10

ShutterSoul


2 Answers

TL;DR: I guess the problem is on the server side of your code (that we don't know yet).

At first: I don't know why it fails for you. I've taken your code and ran it against a public available JSONP API, that returns the current IP of your system and it worked.

Please try yourself using the URL: http://ip.jsontest.com/.

So most probably, the server doesn't return the right response to the JSONP request. Have a look at the network tab in developer tools. With your current code, the answer of the server should be something like:

jsonCallback({'someResponseKeys': 'someResponseValue'});

Note: The header should contain Content-Type:application/javascript!


BTW, even if this doesn't for now solve your problem - here are some tweaks, I'd like to advice to you:

  • Don't set async to false, at the documentation of jQuery.ajax() says:

    Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation.

  • You don't need to set a jsonpCallback, because jQuery will generate and handle (using the success function a random one for you. Quote from the docs:

    This value will be used instead of the random name automatically generated by jQuery. It is preferable to let jQuery generate a unique name as it'll make it easier to manage the requests and provide callbacks and error handling.

So here comes my code:

$(document).ready(function () {
  $("#btnCity").click(function () {
    $.ajax({
      type: "POST",
      url: "http://ip.jsontest.com/",
      crossDomain: true,
      data: { username: "abc", password: "1234" },
      dataType: "JSONP",
      success: function (resdata) {
        console.log("success", resdata);
      },
      error: function (result, status, err) {
        console.log("error", result.responseText);
        console.log("error", status.responseText);
        console.log("error", err.Message);
      }
    });
  });
});

A working example can be found here.

Another solution, like Yonatan Ayalon suggested, can be done with a predefined function and then setting the jsonpCallback explicitly to the function that should be called.

like image 65
ConcurrentHashMap Avatar answered Oct 23 '22 00:10

ConcurrentHashMap


if you see the response in Fiddler, it seems that the issue is in the callback function.

you are doing a jsonP call - which means that you need a callback function to "read" the response data.

Do you have a local function that calls "jsonCallback"?

this is a simple jsonP request, which initiates the function "gotBack()" with the response data:

            function gotBack(data) {
                console.log(data);
            }
            $.ajax({
                url: 'http://test.xyz.com/login' + '?callback=?',
                type: "POST",
                data: formData,
                dataType: "jsonp",
                jsonpCallback: "gotBack"
            });
like image 43
Yonatan Ayalon Avatar answered Oct 22 '22 23:10

Yonatan Ayalon