Javascript 如何使用数据类型为jsonp和负载变量通过ajax获取响应
我有一个外部webservice url,当我使用php curl发送post请求时,我会得到正确的响应,数据是通过发送post字段作为json编码发送的,并给出输出Javascript 如何使用数据类型为jsonp和负载变量通过ajax获取响应,javascript,jquery,ajax,jsonp,payload,Javascript,Jquery,Ajax,Jsonp,Payload,我有一个外部webservice url,当我使用php curl发送post请求时,我会得到正确的响应,数据是通过发送post字段作为json编码发送的,并给出输出 <?php $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => 'http://www.theienable.com/interntest/intern
<?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;
?>
然而,当我使用ajax对同一个webservice url发出post请求时,我没有得到正确的响应,下面是我的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>
我试过你的密码。
其结果是跨站点脚本编写问题
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.
我不知道你的网站是否有访问权限。
看看这里我在ajax代码中给出了数据类型:'jsonp',它不应该给出跨端脚本问题,我这里唯一的问题是如何将json格式的数据发送到Web服务,非常感谢,非常感谢:我使用了data:data,错误是{错误:密码不正确。}ajax调用转换对象就像您填写表单一样。是否确实需要将json格式的数据传递给Web服务?您知道web服务是如何读取参数的吗?这里的问题是我们无法将密码密钥发送到该web服务,您得到的当前响应是正确的,并且它以{错误:密码不正确。}的形式出现,因为它没有获得该密码密钥。我们必须以json对象的形式发送数据,我在curl中这样做了,我得到了正确的输出,我不知道如何在ajax中发送数据。我认为用POST进行JSONP调用是不可能的。它将在GET中转换。看到这个问题并回答:是的,你是绝对正确的,甚至我从一个同事那里知道,无论如何,非常感谢你的帮助,非常感谢:
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.