Solved

Cannot Obtain the Response Message of a jQuery AJAX call (JSON request)

Posted on 2011-02-18
7
1,433 Views
Last Modified: 2012-05-11
In the attached piece of AJAX call through JSON request, I get an alert message saying "Status: success" but cannot get the response message somehow. We are inclined to use the Webservice call request with PHP and AJAX.

However, we do get the response text if tried through RESTClient or POSTER, attached an example screenshot.

Could someone please help?
<script type="text/javascript" src="/scripts/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$.ajax({
    type: 'POST',
    url: 'http://site-name.net:8095/WebService/purchase/markPurchase',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: '{"authorizationToken":"xyz=","purchaseKey":"testmsg", "identifier":"1","purchaseType":"2"}',
    success: function(data, resultState) {
        if (!resultState) {
            alert('No result found!');
        } else {
            alert('Status: ' + resultState);
        }
    },
    error: function(xhr) {
        alert(xhr.statusText);
    },
    complete:function() {
        //
    }
});
</script>

Open in new window

poster-webservice-response.PNG
0
Comment
Question by:ldbkutty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 34924383
check what type of response is returned - xml, html, json...
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34924388
can you share the output of alert(data); in the else section of success handler?

How did you know if you are not getting the response?
0
 
LVL 32

Author Comment

by:ldbkutty
ID: 34924641
With Chrome firebug console, got this error message...

XMLHttpRequest cannot load http://site_name:8095/ECMWebService/purchase/markPurchase. Origin https://www.xyz.com is not allowed by Access-Control-Allow-Origin.

May be the server is thinking that am trying to hack the server through cross-site scripting. http://en.wikipedia.org/wiki/XMLHttpRequest#Cross-domain_requests

If the site_name can whitelist our "xyz" domain in the Access-Control-Allow-Origin setting of web-server, will it be good? I am exploring the Access-Control-Allow-Origin property right now ....
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 82

Expert Comment

by:leakim971
ID: 34924894
You need to use a proxy, cross domain ajax call is not allowed for security purpose, for example this one : http://www.daniweb.com/code/snippet216729.html
0
 
LVL 32

Author Comment

by:ldbkutty
ID: 34925319
the proxy was a great idea, but it also results in the following error:

Failed to load resource: the server responded with a status of 405 (Method Not Allowed) proxy.php?proxy_url=site_name.net:8095/ECMWebService/purchase/markPurchase
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34925376
the proxy_url is a full url (encoded with encodeURIComponent : http://xkr.us/articles/javascript/encode-compare/)  http:
$.ajax({
    type: 'POST',
    url: 'proxy.php?proxy_url=' + encodeURIComponent( "http://site-name.net:8095/WebService/purchase/markPurchase" ),

Open in new window

0
 
LVL 6

Accepted Solution

by:
jjperezaguinaga earned 500 total points
ID: 34933255
Greetings ldbkutty,

Please feel free to research about CORS (Cross-Origin Resource Sharing) and XML HTTP Request Level 2. This is already a W3C working draft.

http://www.w3.org/TR/cors/
http://www.w3.org/TR/XMLHttpRequest2/

Unluckily, XML HttpRequest Level 2 is still not supported as part of the jquery library so you would need to do some code by yourself. However, I think for this case, giving a quick read to CORS will be enough to solve your problem.

Best regards,
-JJ

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question