0

Hi I have to write Rest API call which will serve cross domain request

I have checked with Mozilla (rest client) with request and it serves me data now how to write via jquery/javascript in html.

{"POST to adengine":{"method":"POST","url":"http://xxx.com/Advertisement/1.0/GetAds","body":"aid=Apposaurus_xxx","overrideMimeType":false,"headers":[["Content-Type","application/x-www-form-urlencoded"]]}}

Here is my sample code in html

<html>
<title></title>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">

JSONTest = function() {
    var resultDiv = $("#resultDivContainer");
    $.ajax({
        url: "http://xxx.com/Advertisement/1.0/GetAds",
        type: "POST",
        data:  "aid=Apposaurus_xxx",        
        overrideMimeType:'false',
        crossDomain: 'true',
        dataType: 'jsonp',
        headers:{"Content-Type":'application/x-www-form-urlencoded'},

        success: function (result) {
            switch (result) {
                case true:
                     processResponse(result);
                    break;
                default:
                    resultDiv.html(result);
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        }    
});
};
</script>
</head>
<body>


<div id="resultDivContainer"></div>

<button type="button" onclick="JSONTest();">JSON</button>

</body>
</html> 

Where i am wrong?

4
  • basic variables appid and useragent are missing Did you read the api? Commented Feb 7, 2013 at 6:05
  • When I create a form with the posted values and submit it I don't get valid JSONP back so you must be missing a parameter. I get html with js code back. Works fine in an iframe or setting the src of a script tag but doesn't do much for xmlhttprequest asking for jsonp Commented Feb 7, 2013 at 6:30
  • With headers":[["Content-Type","application/x-www-form-urlencoded"]] it will serve you data. I checked with Mozilla firefox (Rest Plugin) Commented Feb 7, 2013 at 6:35
  • jsfiddle.net you can check your code here, there seems some syntax error Commented Feb 7, 2013 at 6:52

2 Answers 2

0

Cross Domain Request should be accomplished using JSONP. JSONP stands for JSON with Padding. I'll try to explain it here a little.

How do you want to fetch data (script when we talk about JSON) from server? You have two ways:

  1. An ajax request, which is done behind the scene using an instance of XMLHttpRequest object, and which is limited to be sent only to the same domain,
  2. A simple HTTP Get request, which is done via inserting a <script> tag somewhere in the HTML document, mostly in the head section.

The second technique is not limited to Cross-Domain Policy. However, let's examine how browser works as our agent in these two scenarios:

ajax scenario: developer code => browser => request => server => response => browser => developer code. This means that when you use ajax, browser on behalf of you creates an HTTP Request (with X-Requested-With: XMLHttpRequest header field) and sends it to the server, then it gets the response base, *but it gives you the response back, so that as the developer, you have the opportunity to analyse response and do whatever you want to do with it.

HTTP Get scenario: developer code => script tag in head => browser => request => server => response => browser => response execution.

See? When you use JSONP, or when you insert in simple <script> tag in head, browser gets the script back from the server, but it runs script and that's the end. You haven't been hooked into the response, and you don't have control over it. Thus, you need to provide a callback function for it. callback({}) differs from {} in point that when it gets executed, it gives you the opportunity to get the result and do something with it.

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

6 Comments

You are right! What do i need to do for that. Does that means data comes from server should use the JSONP format? Something else as well. Thanks!
datatype is already set to jsonp so that could not be the issue
Not the issue here, please read api.jquery.com/jQuery.ajax where datatype is jsonp as the code posted by op. Issue could be missing request headers, missing post values or the code block in success: function
Can't find api documentation for this service but posting the values through a form just gets me html response not jsonp, maybe it can return jsonp but I could not find how.
|
0

Could you change your code slightly and tell us what is logged to the console? Please do not use IE as it has a horrible implementation of console.log (showing Object.Object). Chrome or Firefox with Firebug will do a better job.

Suggested changes:

        success: function (result) {
console.log("here is the result:",result);
//            switch (result) {
//                case true:
//                     processResponse(result);
//                    break;
//                default:
//                    resultDiv.html(result);
//            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
console.log("this is the error",arguments);
//        alert(xhr.status);
        } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.