简体   繁体   English

如何通过dataType为jsonp和有效负载变量的ajax获取响应

[英]How to fetch response through ajax with dataType as jsonp and with payload variables

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. 我有一个外部Web服务URL http://www.theienable.com/interntest/interntest.php ,当我使用php curl发送发布请求时,我得到了正确的响应,通过将发布字段作为json发送来发送数据编码并给我输出。

  <?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. 我的Web服务O / p如下所示,这是完美的。

{"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. 但是,当我使用ajax iam向相同的Web服务url发出发布请求时,如何无法获得正确的响应,这是我的Ajax代码。

   <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>

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 在这里看看https://en.wikipedia.org/wiki/Cross-site_scripting

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在Ajax中使用数据类型:“ jsonp”获得Java响应,但数据类型:“文本”有效 - Unable to get Java Response in Ajax with dataType: “jsonp” but dataType: “text” Works Jquery / JavaScript - 将Ajax jSONP响应存储到变量中 - Jquery/JavaScript - Store Ajax jSONP response into variables 如何访问 Fetch 响应的负载 - How Do I Access the Payload of a Fetch Response 使用JSONP时,如何捕获jQuery $ .getJSON(或$ .ajax,数据类型设置为&#39;jsonp&#39;)错误? - How do I catch jQuery $.getJSON (or $.ajax with datatype set to 'jsonp') error when using JSONP? 当dataType设置为JSONP时读取JSON响应 - Reading JSON response when dataType is set to JSONP 如何在响应中获取请求有效负载— ajax jquery - How to get the request payload in the response— ajax jquery 如何在jQuery ajax调用上将JSON响应解析为JSONP? - How to parse a JSON response as JSONP on a jQuery ajax call? 每次通过按钮调用Ajax时,如何在jsonp url中重用jsonp响应变量? - how to reuse jsonp response variable in jsonp url each time i call ajax via button? 当用于对Web服务进行Ajax调用的dataType为“ jsonp”时,如何将成功函数的结果与字符串进行比较 - How to compare result of the sucess function with a string when the dataType used to make ajax call to webservice is “jsonp” 提取API:如何访问分块响应的有效负载? - Fetch API: how can I access the payload of a chunked response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM