开发者

getJSON problem (jquery)

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 pleas开发者_运维知识库e?


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");
  }
});


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/.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜