tags:

views:

42

answers:

2

Hi,

I am trying to use getJSON to submit username and password to a webservice and alert "hi"

This is my code:

$.getJSON(address+"?format=json&jsoncallback=?", {CustomerEmail: email, Password: password},
  function(){
     alert("hi");
});

The http headers submit a 200 message so it should be OK but it does not enter the function, does anyone know why please?

+3  A: 

Are the pages located on the same server? Most likely your are violating the same-origin policy.

You could make use of JSONP but .getJSON() expects the parameter to be named callback instead of jsoncallback:

JSONP

If the URL includes the string "callback=?" in the URL, the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.

So to make it work in your case, you could try with $.ajax():

$.ajax({
  url: address+"?format=json",
  data: {CustomerEmail: email, Password: password},
  dataType: "jsonp",
  jsonp: "jsoncallback",
  success: function(data) {
    alert("hi");
  }
});
Felix Kling
then how would I submit the data to the webservice please?
C.
@C.: Oh sorry, I forget to add this, I updated my answer (but actually it is described in the documentation ;))
Felix Kling
+1  A: 

Check, if your received JSON data is valid. Especially, that all strings (even in property names) are properly "quoted".

From the jQuery documentation:

Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason. JSON is a data-interchange format with syntax rules that are stricter than those of JavaScript's object literal notation. For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes. For details on the JSON format, see http://json.org/.

cypheon