1

I have a jsp page which simply prints {"result" : "success"} and there is a php page which calls this jsp with JQUery AJAJAX as below

$(document).ready(function (){ $.ajax({ url: 'index.jsp', data: 'id=test', dataType: 'json', cache: false, async:true, success: function(data) { alert(data); }, error: function(xhr, ajaxOptions, thrownError) { alert('Error ' + xhr.statusText + xhr.responseText); } }); }); 

If I put url in browser it works fine . But this ajax request always goes to error block .

Is there any argument in ajax call wrong ? I get alert 'Error ' no other resoibse text or error code.

I have another exactly same call from a jsp page which worked fine !

5
  • 1
    and what is that alert looking like? Commented Aug 25, 2011 at 16:58
  • 2
    Use Firefox + Firebug - open the "NET" tab and you can see the response from the server directly. Commented Aug 25, 2011 at 17:03
  • what happens if you replace "alert(data)" with "alert(data.result)" or "console.log(data)" ? Commented Aug 25, 2011 at 17:20
  • @Diodeus Yes I already did that it shows empty Commented Aug 25, 2011 at 17:33
  • Then something is wrong with your server-side code. Commented Aug 25, 2011 at 17:39

1 Answer 1

1

I figured it out that it was a cross domain call which was causing issue . I had to use jsonp and add a callback function in order for it to work .

Sign up to request clarification or add additional context in comments.

3 Comments

This is probably the best answer (not the selected one on this page). Had the same issue and running the file on my server instead of locally fixed it. Definitely a cross domain security thing which strangely jquery doesn't give specific error message for.
This answer isn't good enough. It does not show what is meant by "use jsonp and add a callback function".
This is unclear: "use jsonp and add a callback function"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.