[英]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.