0

I have a external webservice url http://www.theienable.com/interntest/interntest.php to which when i send a post request using the php curl i get the correct response, the data is sent by sending the post field as json encoded & gives me the output.

  <?php
  $curl = curl_init();
  curl_setopt_array($curl, array(
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_URL => 'http://www.theienable.com/interntest/interntest.php',
  CURLOPT_USERAGENT => 'Sample Test Request',
  CURLOPT_POST => 1,
  CURLOPT_POSTFIELDS => json_encode(array('password' => 'test12345'))
  ));
  // Send the request & save response to $resp
 $resp = curl_exec($curl);
 echo $resp;
 ?>

And my webservice O/p is given below, which is perfect.

{"error":"","backgroundURL":"http:\/\/www.theienable.com\/interntest\/bgpattern237tfg98gg.png","goButtonURL":"http:\/\/www.theienable.com\/interntest\/gobutton32rf72gf.png","closeButtonURL":"http:\/\/www.theienable.com\/interntest\/closebutton23rg28f.png"}

How ever when i do the post request to the same webservice url using ajax iam not getting an proper response, here is my ajax code.

   <script>

   function requestWebService(){
        var data={"password":"test12345"};
        $.ajax({
        url:"http://theienable.com/interntest/interntest.php",
        data:JSON.stringify(data),
        type:'POST',
        dataType:'jsonp',
        contentType: "application/json",
        async:false,
        success:function(response,xhr){
           $('.resultArea').html(response);
        },
        error:function(xhr,ajaxOptions,thrownError){
        $('.resultArea').html("Response: "+JSON.stringify(xhr));
        }
        });
   } 

 </script>
Srinivasulu Rao
  • 311
  • 1
  • 3
  • 12

1 Answers1

0

I tried your code. The result is a problem of cross-site-scripting.

XMLHttpRequest cannot load http://theienable.com/interntest/interntest.php.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://myserver.local is therefore not allowed access.

I don't know if your site has access or not. Take a look here https://en.wikipedia.org/wiki/Cross-site_scripting

  • i've given the dataType:'jsonp' in the ajax code, it should not give cross side scripting problem, the only problem i have here is how can i send the data in a json format to the webservice, appreciated, thanks a lot :) – Srinivasulu Rao Oct 13 '15 at 10:35
  • I used `data: data` , and the error was `{"error":"Password is incorrect."}` The jquery.ajax call converts object just like you fill a form. Are you sure you need to pass data in json format to the webservice ? Do you know how the webservice reads the parameters ? – Massimiliano Piccinini Oct 13 '15 at 13:41
  • The problem here is we are not able to send the password key to that web service, the current response which you are getting is correct, and its coming as {"error":"Password is incorrect."} because its not getting that password key. We have to send that data as a json object, i did that in curl and i got the correct output, don't know how to send that in ajax. – Srinivasulu Rao Oct 14 '15 at 06:13
  • I think is not possible to make a JSONP call with POST. It will be converted in GET. See this question and answeres : http://stackoverflow.com/questions/3860111/how-to-make-a-jsonp-post-request-that-specifies-contenttype-with-jquery – Massimiliano Piccinini Oct 14 '15 at 13:01
  • Yes you are absolutely right, even i came to know that from one my colleague, anyways thanks a lot for your help, appreciated :) – Srinivasulu Rao Oct 14 '15 at 19:57
  • Hey bro, i gotta a solution for this, i installed a chrome extension to handle the CORS problem and i changed the dataType to 'json' and it worked, hope it may help someone. Thanks again for helping :) – Srinivasulu Rao Oct 15 '15 at 09:44