2009-08-11 8 views
20

मुझे नीचे मेरी jquery स्क्रिप्ट के साथ परेशानी हो रही है, यह एक मूल छीन लिया संस्करण है और यहां तक ​​कि यह काम नहीं करेगा, मेरे पास php फ़ाइल है जो jquery स्क्रिप्ट कॉल करता है करने के लिए, मैंने इसे एक जेसन प्रतिक्रियाएक jquery स्क्रिप्ट से चर में जेएसओएन प्रतिक्रिया कैसे प्राप्त करें

फिर jquery स्क्रिप्ट में इसे मान पढ़ना चाहिए और इसका जवाब देना चाहिए, लेकिन प्रतिक्रिया प्राप्त नहीं हो रही है।

क्या json.response json स्ट्रिंग में एक चर को कॉल करने का गलत तरीका है जो नाम प्रतिक्रिया है?

कर सकते हैं किसी को मदद कृपया मुझे

<?PHP 
header('Cache-Control: no-cache, must-revalidate'); 
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
header('Content-type: application/json'); 

// set to retunr response=error 
$arr = array ('resonse'=>'error','comment'=>'test comment here'); 
echo json_encode($arr); 
?> 

//the script above returns this: 
{"response":"error","comment":"test comment here"} 

<script type="text/javascript"> 
$.ajax({ 
    type: "POST", 
    url: "process.php", 
    data: dataString, 
    dataType: "json", 
    success: function (data) { 
     if (json.response == 'captcha') { 
      alert('captcha'); 
     } else if (json.response == 'error') { 
      alert('sorry there was an error'); 
     } else if (json.response == 'success') { 
      alert('sucess'); 

     }; 
    } 

}) 
</script> 

अद्यतन अटक कर रहा हूँ;

मैं
json.response

बदल दिया है

data.response

में लेकिन यह यह ork या तो

उत्तर

27

यहां लिपि है, उपरोक्त सुझावों का उपयोग करने के लिए पुनः लिखा गया है और आपकी नो-कैश विधि में बदलाव है।

<?php 
// Simpler way of making sure all no-cache headers get sent 
// and understood by all browsers, including IE. 
session_cache_limiter('nocache'); 
header('Expires: ' . gmdate('r', 0)); 

header('Content-type: application/json'); 

// set to return response=error 
$arr = array ('response'=>'error','comment'=>'test comment here'); 
echo json_encode($arr); 
?> 

//the script above returns this: 
{"response":"error","comment":"test comment here"} 

<script type="text/javascript"> 
$.ajax({ 
    type: "POST", 
    url: "process.php", 
    data: dataString, 
    dataType: "json", 
    success: function (data) { 
     if (data.response == 'captcha') { 
      alert('captcha'); 
     } else if (data.response == 'success') { 
      alert('success'); 
     } else { 
      alert('sorry there was an error'); 
     } 
    } 

}); // Semi-colons after all declarations, IE is picky on these things. 
</script> 

मुख्य मुद्दा यहाँ है कि आप JSON में कोई गलती आप लौटने थे (बजाय "प्रतिक्रिया" के "resonse" था। इसका मतलब था कि आप जावा स्क्रिप्ट कोड में गलत प्रॉपर्टी देख रहे थे। एक तरह से भविष्य में इन समस्याओं को पकड़ने के data का मूल्य console.log और यह सुनिश्चित करें संपत्ति आप देख रहे हैं न हो।

Learning how to use the Chrome debugger tools है (या फ़ायरफ़ॉक्स/सफारी/ओपेरा/आदि। में इसी तरह के उपकरण) भी अमूल्य होगा।

5

आप अपने जे एस में data.response बजाय json.response का उपयोग करना चाहिए नहीं किया ।

$arr = array ('resonse'=>'error','comment'=>'test comment here'); 

सूचना गलत वर्तनी "resonse":

+0

मैंने अभी कोशिश की लेकिन यह अलग नहीं हुआ – JasonDavis

+0

क्या आप यह जांच सकते हैं कि आपका डेटा किस प्रकार का है। 'अलर्ट (टाइपफ डेटा); सफलता कॉलबैक में इसे करना चाहिए। – RaYell

+0

यह कहता है "ऑब्जेक्ट" – JasonDavis

4

आपका पीएचपी सरणी के रूप में परिभाषित किया गया है। इसके अलावा, जैसा कि राययल ने उल्लेख किया है, आपको अपने success फ़ंक्शन में json के बजाय data का उपयोग करना होगा क्योंकि इसका पैरामीटर वर्तमान में data है।

वर्तनी फ़ॉर्म resonse से response बदलने के लिए अपनी PHP फ़ाइल को संपादित करने का प्रयास करें। यह तब काम करना चाहिए।

+0

धन्यवाद कि समस्या थी, मैंने इसे स्क्रिप्ट के 1 क्षेत्र में पकड़ा लेकिन मुझे वह हिस्सा याद आया – JasonDavis

संबंधित मुद्दे