How to process json response from ajax call

前端 未结 1 1938

My webservice returns a JSON object like following

[\"abc\",\"xyz\",\"option_3\"]

i.e. when I put this address in chrome browser http://l

相关标签:
1条回答
  • 2021-01-25 07:22

    Update

    You edited the question; you can't use dataType: 'json' because of cross-domain issues that can only be overcome by either using a proxy on the same domain or via CORS.

    When you use dataType: 'jsonp' you can't use async: false; this is because it doesn't really use XMLHttpRequest but uses the <script> tag to make it do cross-domain. I believe jQuery simply ignores the async setting without issuing a warning.

    Therefore, the alert() will always be empty and you should move it inside the success callback.

    To support JSONP your web service must wrap the returned data into a function call, identified by the callback GET parameter, e.g.

    callback([1, 2, 3])
    

    If your web service doesn't support this, as mentioned before, you would need to use CORS, e.g. add this response header:

    Access-Control-Allow-Origin: *
    
    0 讨论(0)
提交回复
热议问题